2014-02-14 106 views
-2
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConTempConversion_Michelle 
{ 
    class Program 
    { 
     static void GetChoice(ref int ichoice) 
     { 
      Console.Write("Enter choice: "); 
      string input = Console.ReadLine();// get string from the user 

       bool result = int.TryParse(input, out ichoice);//convert input from  string to ichoice int value 

      if (!result) 
      { 
      while (!result && ichoice > 3) 
      { 
       Console.WriteLine("Invalid value.Try again:"); 

       input = Console.ReadLine(); 

       result = int.TryParse(input, out ichoice); 
      } 
      } 
    } 

    static void Main(string[] args) 
    { 
     double fTemp; 
     double cTemp; 
     double convertToC; 
     double convertToF; 

     Console.WriteLine("Welcome to the temperature conversion application"); 
     Console.WriteLine("_________________________________________________"); 
     Console.WriteLine("1. Fahrenheit to Celsius"); 
     Console.WriteLine("2. Celsius to Fahrenheit"); 
     Console.WriteLine("3. Exit"); 

     int ichoice = 0; 
     GetChoice(ref ichoice); 
     do 
     { 

      if (ichoice == 1) 
      { 
       Console.WriteLine("Enter Fahrenheit temperature: "); 
       fTemp = int.Parse(Console.ReadLine()); 
       convertToC = ConvertCelcius(fTemp); 
       Console.WriteLine(fTemp + "Fahrenheit is " + convertToC + "Celsius"); 
       Console.WriteLine("Welcome to the temperature conversion application"); 
       Console.WriteLine("_________________________________________________"); 
       GetChoice(ref ichoice); 
      } 
      if (ichoice == 2) 
      { 
       Console.WriteLine("Enter Celsius temperature: "); 
       cTemp = int.Parse(Console.ReadLine()); 
       convertToF = ConvertCelcius(cTemp); 
       Console.WriteLine(cTemp + "Celsius is " + convertToF+ "Fahrenheit"); 
       Console.WriteLine("Welcome to the temperature conversion application"); 
        Console.WriteLine("____________________________________________________"); 
       GetChoice(ref ichoice); 

      } 
      if (ichoice == 3) 
      { 
       Console.WriteLine("Thank you for using the temperature conversion application. Please come again."); 
      } 
      else 
      { 
       Console.WriteLine("Invalid choice. Please choose again!"); 
      } 

     } 
     while (ichoice < 3); 
     Console.ReadKey(); 

    } 
    static double ConvertCelcius(double c) 
    { 
     double f; 

     return f = 9.0/5.0*c + 32; 
    } 
    static double ConvertFahrenheit(double f) 
    { 
     double c; 

     return c = (f - 32) * (5.0/9.0); 

     } 
    } 
} 

我只是有這個程序的輸出問題。從C到F的轉換是正確的,但從F到C不正確。數學公式是正確的,但爲什麼輸出不是?請幫助...謝謝。C#溫度轉換(1期)

米歇爾

+2

什麼是你的輸入和輸出?你期望結果如何?你的程序給你什麼結果? –

+0

輸出是三個3選項,1 F隱藏到C,2 C轉換爲F,3退出程序。如果輸入這四個選項中的任何一個,比如4,5,那麼它會提醒用戶這是一個無效輸入,請重試。 – Michelle

+2

在選擇1和2中調用相同的ConvertCelcius。必須是拼寫錯誤。 – Yuan

回答

7

你調用錯誤的方法:

 if (ichoice == 2) 
     { 
      Console.WriteLine("Enter Celsius temperature: "); 
      cTemp = int.Parse(Console.ReadLine()); 
      convertToF = **ConvertCelcius**(cTemp); 
      Console.WriteLine(cTemp + "Celsius is " + convertToF+ "Fahrenheit"); 
      Console.WriteLine("Welcome to the temperature conversion application"); 
       Console.WriteLine("____________________________________________________"); 
      GetChoice(ref ichoice); 

     } 
+0

是的,這將做到 – Nick

+0

我不會投票給你,但我覺得你可以花更多的精力來解答這個問題。這個問題沒有得到很好的解釋,而且OP顯然正在努力解決基本問題 – musefan

+4

我認爲OP沒有在基礎上掙扎,他們只是做了一個拼寫錯誤的拼寫錯誤。我還能寫些什麼來向他們解釋他們正在調用錯誤的方法?感謝沒有投我的意見:) – Nick