![]() |
Slight problem with my code
I'm writing a program where I'm writing my own method to calculate something. The user is asked to enter 3 integers, and then the program calls a method called Sum to compute the sum and display it. But I'm getting a compiler error. I don't know why since I'm taking a concept from my book and just applying it to my own program, but what I got written is pretty much exactly the same way the program looks in the book. Here is my program:
:
using System;But the compiler flags the word "int" right after the word "static" in my method definition and says something about a class, delegate, enum, or struct is expected. So I'm kind of confused as to why there is a problem. |
the Sum() function or any for that matter needs to be in the Program class or a new class/struct etc. ie...
:
P.S the code will compile then but there are a couple of things: Console.WriteLine("The sum is ", Sum(number1, number2, number3)); // "," seperates the parameters passed so unless the WriteLine() function's second parameter also displays text it won't work // should be Console.WriteLine("The sum is " + Sum(number1, number2, number3)); // + to join the string and functions return value ^-- if that's how it was in the book and it works sorry, the book > me =P * might want to think about adding a Console.ReadKey() so you can see the results :P |
Yeah, I just discovered that little error. My program works good now, but how do you split a long line of code into two lines? For example I have this line of code that is rather long:
:
Console.WriteLine("{0}{1}", "The largest is ", Maximum(number1, number2, number3));Not only is it too long to print on one line when I print my code out, but even in the editor window it looks too long. I tried one method of breaking it up but the compiler wouldn't go for it. |
:
Console.WriteLine("{0}{1}", "The largest is ",Just hit enter :P . Unless you're in a string , the compiler won't mind. |
| All times are GMT -5. The time now is 2:23 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC