1
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;
        }
    }

私は答えが本当に簡単であることを知っています。私が間違ったことを理解できないようです。

私は温度を摂氏法に渡そうとしています。

4

4 に答える 4

1

メソッドを静的としてマークします。

public static int Celsius(int t)
于 2013-01-18T14:46:51.580 に答える
0

問題は、Celsiusメソッドが のように静的ではないことMainです。

この2つの方法で解決できます。

静的Celsiusにする:

public static int Celsius(int t)

のインスタンスを作成してから、次のProgramように呼び出しますCelsius

var program = new Program();   
program.Celsius(temp);
于 2013-01-18T14:47:10.323 に答える
0

staticあなたの方法の方法で試してくださいCelcuis。呼び出し元メソッドと同じクラスのメソッドを呼び出したい場合、および直接呼び出したい場合はstatic、メソッドでキーワードを使用する必要があります。このような;

static public int Celsius(int t)
{
    int c;
    c = 5 / 9 * (t - 32);
    return c;
}

他のオプションについては、クラス インスタンスを作成し、if 条件でメソッドを呼び出すことができます。このような;

if ( choice == "c" )
{
   Program p = new Program();
   p.Celsius(temp);
}
于 2013-01-18T14:47:43.317 に答える
0

1 つの可能性は、Celsius メソッドを static にすることpublic static int Celsius(int t)です。
もう 1 つは、新しいインスタンスを作成してProgram呼び出すCelsius方法です。

new Program().Celsius(temp);
于 2013-01-18T14:49:28.243 に答える