2017-09-25 116 views
1

我構建控制檯應用程序以提高我的c#技能。我有兩個問題,我似乎無法在網上找到。在switch語句中顯示Console.ReadLine()

問題1:我想顯示的任何用戶輸入的輸出(用戶輸入一個數字值。當程序運行並顯示一個答案,我要被顯示在用戶輸入以及)..

問題2:目前您必須在第一次輸入後按兩次Enter鍵繼續。有沒有辦法只需按一次回車鍵?

在此先感謝!

下面是現有的代碼(我會包括類只是櫃面你需要某些原因,第二):

namespace StaticDemo { 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string selection = string.Empty; 
      while (selection != "q" && selection != "Q") 
      { 
       Console.Write("Enter C)elsius to Fahrentheit or F(ahrenheit to Celsius or Q(uit. \nSelect C, F, or Q then press the enter key twice to continue: "); 
       selection = Console.ReadLine(); 

       double fahrenheit = 0, celsius = 0; 
       string line = Console.ReadLine(); 
       switch (selection) 
       { 

        case "C": 
        case "c": 
         Console.Write("Please enter the Celsius temperature that you want converted to a Fahrenheit temperature: "); 
         fahrenheit = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine()); 
         Console.WriteLine($"The temperature you provided in celsius: {line}, would be: {fahrenheit} in fahrentheit"); 
         break; 

        case "F": 
        case "f": 
         Console.Write("Please enter the Farentheit temperature that you want converted to a Celsius temperature: "); 
         celsius = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine()); 
         Console.WriteLine($"The temperature you provided in Fahrentheit: {line}, would be: {celsius} in celsius"); 
         break; 

        case "Q": 
        case "q": 
         break; 
        default: 
         Console.WriteLine("Enter C F or a Q, Moron!"); 
         break; 

       } 
       Console.WriteLine(); 
       Console.WriteLine(); 
      } 
     } 
    } } 

第二類代碼如下,如果你需要它無論出於何種原因:

namespace StaticDemo 
{ 
    class TemperatureConverter 
    { 
     public static double CelsiusToFahrenheit(string tempCelsius) 
     { 
      double celcius = double.Parse(tempCelsius); 
      double fahrenheit = (celcius * 9/5) + 32; 
      return fahrenheit; 
     } 
     public static double FahrenheitToCelsius(string tempFahrenheit) 
     { 
      double fahrenheit = double.Parse(tempFahrenheit); 
      double celsius = (fahrenheit - 32) * 5/9; 
      return celsius; 
     } 
    } 
} 
+0

好的,你在循環中有兩個readline調用。 – OldProgrammer

回答

3

此行是無用的,它是處理選擇

string line = Console.ReadLine(); 

您已經閱讀選擇變量兩行了所需的兩個輸入的原因。

要獲得由用戶所提供的溫度的值,就需要獲得輸入,而無需直接傳遞到轉換類

Console.Write("Please enter the Celsius temperature that you want converted to a Fahrenheit temperature: "); 
    string input = Console.ReadLine(); 
    fahrenheit = TemperatureConverter.CelsiusToFahrenheit(input); 
    Console.WriteLine($"The temperature you provided in Celsius: {input}, would be: {celsius} in Fahrenheit"); 

當然同樣適用於代碼的其它塊,其將華氏溫度到攝氏溫度。還請注意,我認爲你已經顛倒了信息。

+0

史蒂夫,非常感謝!第一。您在選擇F時顯示錯誤的華氏溫度信息是正確的。我只是複製粘貼並忘記切換方法。你建議的一切都有效!非常感謝! – Jbbanuelos

0

你基本上要求用戶輸入兩次。

Console.Write("Enter C)elsius to Fahrentheit or F(ahrenheit to Celsius or Q(uit. \nSelect C, F, or Q then press the enter key twice to continue: "); 
selection = Console.ReadLine().ToUpper(); 
Console.WriteLine("Please enter the temperature that you want converted: "); 
string tempToConvert = Console.ReadLine(); 

switch (selection) 
{ 
    case "C":      
     fahrenheit = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine()); 
        Console.WriteLine($"The temperature you provided in celsius: {tempToConvert }, would be: {fahrenheit} in fahrentheit"); 
        break; 

考慮使用ToUpper的或ToLower將用戶輸入的,所以你不必擔心輸入的格式,當你讓你的案件。

+0

哦,非常好!我從來不知道!很有用!非常感謝,山姆! – Jbbanuelos

0

進行上述更改你不需要這行之後的行

selection = Console.ReadLine(); 

更改爲

selection = Convert.ToString(Console.ReadKey()); 

也是我的想法。

string line = Console.ReadLine(); 
+0

謝謝我已閱讀它,我想你是指代碼片段的格式..做了一些修改..讓我知道它現在看起來如何.. –

+0

這是好多了,downvote刪除 –

0

這是因爲行

string line = Console.ReadLine(); 

將其更改爲

string line; 

我覺得你以前想該行讀取值來轉換。