【问题标题】:How to prompt the user to continue?如何提示用户继续?
【发布时间】: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


【解决方案1】:

首先,让我们提取方法ReadInt(为什么要重复自己?):

private static int ReadInt(string title) {
  // keep asking user until correct value provided
  while (true) {
    if (!string.IsNullOrWhiteSpace(title))
      Console.WriteLine(title);

    // If we can parse user input as integer...
    if (int.TryParse(Console.ReadLine(), out int result))
      return result; // .. we return it

    Console.WriteLine("Syntax error. Please, try again"); 
  }
}

主代码:在这里,您可以将代码片段包装成do {...} while(因为您希望循环至少执行一次一次)

do {
  int num1 = ReadInt("Enter a number to add: ");
  int num2 = ReadInt("Enter another number to add: ");
        
  // (long) num1 - we prevent integer overflow (if num1 and num2 are large)
  Console.WriteLine($" {num1} + {num2} = {(long)num1 + num2}");

  Console.WriteLine("Do You want to add more numbers? Y / N");
}
while(Console.ReadKey().Key == ConsoleKey.Y); 

【讨论】:

  • +1 用于提取方法并使其保持干燥。但是,do {...} while 的 while 部分不应该只是 while (Console.ReadKey().Key == ConsoleKey.Y); 吗?
  • @The2Step:恕我直言,我们有一个典型的do {..} while():我们希望循环至少执行一次。请注意,while() {...} 根本无法执行。
  • 对不起,我想我说错了,我同意你在这种情况下使用do {...} while。但是,我指的是它在while 部分中引发的错误。你打电话给Console.ReadKey 两次,我说我认为应该是(Console.ReadKey().Key == ConsoleKey.Y)。
  • @The2Step:我明白了!我的错,你说得对,我已经编辑了答案
  • 谢谢大家,效果很好。我只是想知道这些线路是如何工作的。我虽然你需要 Console.Write() 在屏幕上获取文本? int num1 = ReadInt("请输入要添加的数字:"); int num2 = ReadInt("请输入另一个要添加的数字:");
【解决方案2】:
  while (true)
  {
    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.WriteLine("Do You want to add more numbers? Y / N");

    string YesOrNo = Console.ReadLine();

    if (YesOrNo == "N")
    {
      break;
    }
  }

您需要一个循环,我在这种情况下看到的最常见的就是 while 循环。但是,您还需要一个中断条件,以便 while 循环结束 if 语句是您可以像上面的示例一样中断的一种方式。如果用户在控制台中输入 N,则到达中断。

请注意,如果在控制台中输入 N 以外的任何内容,我的示例将继续运行。

【讨论】:

  • 谢谢,这有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 2019-05-07
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
相关资源
最近更新 更多