C# 如何允许用户在if语句中输入新值
我希望有人能帮助我,告诉我应该使用什么代码,以防输入的数字是第二个if或第三个if,如果用户没有输入正确的数量(73-77),那么我希望用户能够输入一个新的值,我可以使用。。。请问我该怎么做C# 如何允许用户在if语句中输入新值,c#,if-statement,C#,If Statement,我希望有人能帮助我,告诉我应该使用什么代码,以防输入的数字是第二个if或第三个if,如果用户没有输入正确的数量(73-77),那么我希望用户能够输入一个新的值,我可以使用。。。请问我该怎么做 namespace test { class Program { public static int FahrToCels(int fahr) { int cel = (fahr - 32) * 5 / 9;
namespace test
{
class Program
{
public static int FahrToCels(int fahr)
{
int cel = (fahr - 32) * 5 / 9;
return cel;
}
static void Main(string[] args)
{
Console.WriteLine("write down temprature: ");
int fahrenheit = int.Parse(Console.ReadLine());
int celsius = FahrToCels(fahrenheit);
Console.Write("Press any key to continue . . .");
Console.ReadKey(true);
do
if (celsius >= 73 && celsius <= 77)
{
Console.WriteLine("now it works ");
}
else if (celsius < 72)
{
Console.WriteLine("");
}
else if (celsius > 77)
{
Console.WriteLine("");
}
while (true);
}
}
}
名称空间测试
{
班级计划
{
公共静态内部FahrToCels(内部fahr)
{
int cel=(华氏32度)*5/9;
返回cel;
}
静态void Main(字符串[]参数)
{
控制台。写线(“写下温度:”);
int fahrenheit=int.Parse(Console.ReadLine());
整数摄氏度=华氏度(华氏度);
控制台。写入(“按任意键继续…”);
Console.ReadKey(true);
做
如果(摄氏度>=73和摄氏度77)
{
控制台。写线(“”);
}
虽然(正确);
}
}
}
如果您想确保摄氏度
在[73..77]
范围内,您可以尝试
static void Main(string[] args) {
int fahrenheit;
int celsius;
// Keep asking user until (s)he enters a valid celsius value
while (true) {
Console.WriteLine("Skrive in Temperaturen: ");
// check if value a valid integer, and not say "bla-bla-bla"
if (!int.TryParse(Console.ReadLine(), out fahrenheit)) {
Console.WriteLine("Not an integer value, please, try again");
continue;
}
// fahrenheit is a valid integer value, we can compute corresponding celsius...
celsius = FahrToCels(fahrenheit);
// ... and validate the celsius value then
if (celsius < 73)
Console.WriteLine("Too low temperature (below 73 degree Celsius)");
else if (celsius > 77)
Console.WriteLine("Too high temperature (above 77 degree Celsius)");
else
break; // celsius is valid integer value in [73..77] range
}
// From now on celsius contains integer in [73..77] range
Console.WriteLine("Now it works");
Console.Write("Press any key to continue . . .");
Console.ReadKey(true);
}
static void Main(字符串[]args){
内华氏度;
摄氏整数度;
//不断询问用户,直到用户输入有效的摄氏度值
while(true){
控制台写入线(“温度驱动:”;
//检查值是否为有效整数,不要说“bla bla bla”
如果(!int.TryParse(Console.ReadLine(),out fahrenheit)){
WriteLine(“请不要是整数值,再试一次”);
继续;
}
//华氏温度是一个有效的整数值,我们可以计算相应的摄氏度。。。
摄氏度=华氏度(华氏度);
//…然后验证摄氏度值
如果(摄氏度<73)
控制台。WriteLine(“温度过低(低于73摄氏度)”);
否则,如果(摄氏度>77)
控制台。WriteLine(“温度过高(高于77摄氏度)”);
其他的
break;//摄氏度是[73..77]范围内的有效整数值
}
//从现在起,摄氏度包含[73..77]范围内的整数
Console.WriteLine(“现在可以了”);
控制台。写入(“按任意键继续…”);
Console.ReadKey(true);
}
您所拥有的有几个主要问题。附件是我相信您正在寻找的工作代码,尽管在ifs中,您的摄氏度范围是70年代,当您的华氏度到摄氏度转换器将值转换为摄氏度时,该值将在20年代范围内
namespace test
{
class Program
{
public static int FahrToCels(int fahr)
{
int cel = (fahr - 32) * 5 / 9;
return cel;
}
static void Main(string[] args)
{
int celsius;
int fahrenheit;
do
{
Console.WriteLine("Skrive in Temperaturen: ");
fahrenheit = int.Parse(Console.ReadLine());
celsius = FahrToCels(fahrenheit);
Console.Write("Presifs any key to continue . . .");
Console.ReadKey(true);
if (celsius >= 22 && celsius <= 28)
{
Console.WriteLine("now it works ");
//Insert whatever here
break;
}
else if (celsius < 22)
{
//Something to tell them they need to retry
}
else if (celsius > 28)
{
//Something to tell them they need to retry
}
}
while (celsius >= 22 && celsius <= 28);
}
}
}
名称空间测试
{
班级计划
{
公共静态内部FahrToCels(内部fahr)
{
int cel=(华氏32度)*5/9;
返回cel;
}
静态void Main(字符串[]参数)
{
摄氏整数度;
内华氏度;
做
{
控制台写入线(“温度驱动:”;
fahrenheit=int.Parse(Console.ReadLine());
摄氏度=华氏度(华氏度);
Console.Write(“提示任何键继续…”);
Console.ReadKey(true);
如果(摄氏度>=22和摄氏度28)
{
//告诉他们需要重试的内容
}
}
虽然(摄氏>=22&&摄氏度啊,我会尝试一下。非常感谢您的时间和回答^^^谢谢,我会尝试第一个代码,如果它不是我真正希望的,我会尝试您的代码,无论如何,您的答案非常合适:)