Originally Posted by
chaosweapon
ICan someone please explain the static void main? I've used it a couple of times but I don't know what it means.
A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.
void simply means that the method returns nothing. You can have an int method that would return an int, for instance. Void methods do not return anything at all.
So, what does this all mean? The main method is static for multiple reasons. The easy explaination is because it's the method called at the start of the program before any others that you create. I could go into a lengthy discussion explaining the difference between static and instance members, but that is probably beyond the scope of where you are at this moment. Basically, if you make a method static, it's the same every time you call it. For a variable, if it is static, it is the same no matter how many copies of that class you make. If you change that variable in one of the instances, it changes in all of them.
Originally Posted by
Unknown Entity
The task we've been given was to make a program which converts cm into inches (1cm = 2.57inches). The thing is, I'm not sure about the symbols I should be using for this (* / - ...). Now, as I need to work this out on my own, I don't want an answer - just a little guidence to push me on the right path. Mainly because so far all I have is:
Well there's multiple ways to do it really. You can have a variable that has the upconversion rate in it or write a method to do it.
Here's a simple method that would do it:
Code:
public static double CentimetersToInches(string input)
{
// Convert argument to double for calculations.
double c = System.Double.Parse(input);
// Convert Centimeters to Inches
double i = (c * 2.57);
return i;
}
OR:
Outside of your main method (inside the main class):
Code:
static final double CENTIMETERSTOINCHES = 2.57;
then just take your input and multiply:
Code:
double i = input * CENTIMETERSTOINCHES;
I think your main hangup is you don't seem to know how to use variables yet. All your calculations are typically done with variables. Well you could technically do the whole program with none, but that'd be a mess and nobody would want to debug it:
Code:
System.Console.Writeln("Input the number of centimeters:");
System.Console.Writeln("Centemeters to inches is {0}", (System.Double.Parse(System.Console.ReadLine())* 2.57);
Yeah, I'd bust you upside the head for writing something like that. Variables are what you use to store values and information. Use them!
Double is your standard floating-point number variable. If you need to store a number with decimals, use double. Int is used for your typical non-floating-point numbers... ie: it does not store decimals. Keep that in mind... because it WILL screw up your calculations if you don't.
Code:
System.Console.Writeln("5 / 2.2 is: " + ((int)5 / (int)2.2));
//5 / 2 is: 2
Stuff like that will screw you over every time. Don't depend on the compiler to tell you. Many cases it will flag it as "loss of precison". If it does, you are trying to dump a bigger value into the wrong sized container... like a golf ball through a garden hose. It doesn't fit. You can make it fit, but it's not going to be pretty.
Bookmarks