C#根据输入重复或关闭程序 - 温度转换

时间:2014-10-24 01:03:18

标签: c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("What is the Temperature in Fahrenheit?")
           string input = Console.ReadLine ();
            double tt = double.Parse (input);
            double cc = (tt-32)*5/9;
            Console.WriteLine(cc + " is the temperature in Celsius");

        }
    }
}

我的问题是,如果不这样做,我需要接收输入并关闭。如果他们的输入是(“”),程序需要关闭,但如果他们输入一个数字 - 例如13,我需要让程序循环回到开头,直到我得到(“”)的输入,此时程序关闭。我对编程很新,但我已经尝试了所有我能想到的东西来尝试/弄清楚,所以你能给予的任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:2)

尝试:

Console.WriteLine("What is the Temperature in Fahrenheit?")
string input = Console.ReadLine ();

while (input != "")
{
    double tt = double.Parse (input);
    double cc = (tt-32)*5/9;
    Console.WriteLine(cc + " is the temperature in Celsius");
    Console.WriteLine("What is the Temperature in Fahrenheit?")
    input = Console.ReadLine ();
}

答案 1 :(得分:0)

如果您使用特定字符指定结束程序而不是空行,则相对容易:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string exit = "";
            while(exit.ToLower != "no") 
            {
                Console.WriteLine("What is the Temperature in Fahrenheit?");
                string input = Console.ReadLine();
                double tt = double.Parse (input);
                double cc = (tt-32)*5/9;
                Console.WriteLine(cc + " is the temperature in Celsius");
                Console.WriteLine("if you want to do another conversion, enter \"yes\" otherwise, enter \"no\"");
                exit = Console.ReadLine();
            }
        }
    }
}