【问题标题】:Break Loop on pressing =按下时中断循环 =
【发布时间】:2022-10-01 00:15:50
【问题描述】:

在没有高级代码的情况下按下 = 而不是 0 时如何打破循环,因为我还在开始

Here\'s the code

  • 请将您的代码作为文本添加到您的问题中(并注意code formatting)。有many good reasons,为什么代码图像不是一个好主意。最重要的原因:我们无法将图像中的代码复制到我们的环境中进行测试和改进,这使得帮助您变得更加困难。
  • 我们更喜欢将代码发布为文本,而不是图像。单击编辑链接。

标签: c#


【解决方案1】:

而不是像这样从Console.ReadLine() 获得int 获得string:

string input = Console.ReadLine();

然后检查它是否是= 并打破:

if(input == "=")
{
    break;
}

在他们没有输入= 的情况下,您仍然可以将其用作数字,完整代码类似于

int sum = 0;

while(true)
{
    Console.Write("Enter a number (or = to exit)");
    string input = Console.ReadLine(); // Take input as string
    
    if(input == "=") // If they entered =, break
    {
        break;
    }

    sum += Convert.ToInt32(input); // If they entered a number, convert it to integer
}

Console.WriteLine($"The sum of all the numbers is: {sum}");

【讨论】:

  • @MuhammadAtchinxo 你不是要求用“=”符号而不是 0
  • 是的,对不起,我不得不刷新页面完整的回复没有显示
猜你喜欢
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多