class Program
{
static void Main(string[] args)
{
int temp;
string choice;
int finalTemp;
Console.WriteLine("Enter a temperature");
temp = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Convert to Celsius or Fahrenheit?" + "\n" +"Enter c or f");
choice = Console.ReadLine();
if (choice == "c")
{
Celsius(temp);
}
Console.ReadLine();//to keep open
} //Main End
public int Celsius(int t)
{
int c;
c = 5 / 9 * (t - 32);
return c;
}
}
I know the answer is really simple I just can't seem to figure out what I have done wrong.
I'm trying to pass temp to the Celsius method.
将您的方法标记为静态:
public static int Celsius(int t)
The problem is that the Celsius
method is not Static, like Main
.
You could resolve this 2 ways.
Make Celsius
static:
public static int Celsius(int t)
Create an instance of the Program
and then call Celsius
:
var program = new Program();
program.Celsius(temp);
Try with static
method of your Celcuis
method. If you want to call a method in the same class with your caller method and if you want to call directly you should use static
keyword on your method. Like this;
static public int Celsius(int t)
{
int c;
c = 5 / 9 * (t - 32);
return c;
}
For other option, you can create a class instance and call your method in your if condition. Like this;
if ( choice == "c" )
{
Program p = new Program();
p.Celsius(temp);
}
One possibility is to make the Celsius method static public static int Celsius(int t)
.
Another, is to create a new instance of Program
and call Celsius
on it:
new Program().Celsius(temp);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.