在switch语句中显示Console.ReadLine()

时间:2017-09-25 17:03:14

标签: c#

我正在构建控制台应用程序以提高我的c#技能。我有两个问题似乎无法在网上找到。

问题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;
        }
    }
}

4 个答案:

答案 0 :(得分: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");

当然,这同样适用于将华氏温度转换为摄氏温度的其他代码块。另请注意,我认为您已将信息反转。

答案 1 :(得分: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,这样您在制作案例时就不必担心输入的格式。

答案 2 :(得分:0)

更改行

selection = Console.ReadLine();

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

我认为在做出上述改变后你不需要这一行。

string line = Console.ReadLine();

答案 3 :(得分:0)

这是因为行

string line = Console.ReadLine();

将其更改为

string line;

我认为您之前希望该行读取要转换的值。

相关问题