我構建控制檯應用程序以提高我的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;
}
}
}
好的,你在循環中有兩個readline調用。 – OldProgrammer