【发布时间】:2022-01-19 22:54:20
【问题描述】:
我想问用户是否要继续添加两个数字,所以如果他们输入 Y 它将重新开始,如果 N 它会退出。
我不确定是否应该使用 if/else 或 while(或两者!)或其他什么。
这是我目前所拥有的:
Console.Write("Enter a number to add: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter another number to add: ");
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = num1 + num2;
string num11 = Convert.ToString(num1);
string num22 = Convert.ToString(num2);
Console.WriteLine(" " + num1 + " + " + num2 + " = " + num3);
Console.Write("Do You want to add more numbers? Y / N");
Console.ReadLine();
我怎样才能完成它?
【问题讨论】:
-
你需要一个循环。哪个不太重要。您可以使用
while(1)永久循环,然后使用if( Console.ReadLine() == "N" ) { break; }。
标签: c# while-loop console-application do-while