[英]C# repeat or close program based on input - Temperature Conversion
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,則需要使程序循環回到開頭,直到得到(“”)的輸入為止。程序關閉。 我對編程還很陌生,但是我已經嘗試了所有可以想到的嘗試/嘗試,因此,您可以提供的任何幫助將不勝感激。
嘗試:
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 ();
}
相反,如果您使用特定字符指定結束程序而不是空行,則相對容易:
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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.