Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-程序未退出_C#_Exit - Fatal编程技术网

c#-程序未退出

c#-程序未退出,c#,exit,C#,Exit,刚开始使用C#,这可能只是一个简单的修复,但我似乎看不到它。一旦程序第一次执行(添加两个数字后),系统将提示用户输入是或否作为退出条件。当我输入“否”时,程序将再次循环 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BasicCalculator { class Program

刚开始使用C#,这可能只是一个简单的修复,但我似乎看不到它。一旦程序第一次执行(添加两个数字后),系统将提示用户输入是或否作为退出条件。当我输入“否”时,程序将再次循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            bool again = false;
            while (!again)
            {
                Console.WriteLine("C# Basic Calcuator");
                Console.WriteLine("Please enter two numbers to be added: ");
                Console.WriteLine("Number 1: ");

                int result;
                int a = Convert.ToInt32(Console.ReadLine());


                Console.WriteLine("Number 2: ");
                int b = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Result: ");
                result = a + b;

                //print result
                Console.WriteLine(result);

                //potential exit condition
                Console.WriteLine("Would you like to calculate again?");
                if(Console.ReadLine() == "no")
                {
                    again = false;
                }
                else
                {
                    again = true;
                }

            }



        }
    }
}
非常感谢您的帮助

变化

while (!again)

当用户键入no时,
再次设置为false;因此,在
while
循环中,再次被否定,从而导致while循环的继续

顺便说一句,你需要改变

bool again = false;


将顶部的两行更改为以下内容:

bool again = true;
while (again)

意外编程电源激活

if(Console.ReadLine() == "no")
    again = true;
else
    again = false;
此外,您可能希望将变量的名称更改为类似于
stop\u flag
的名称,这样才有意义。

您可以编写

again = (Console.ReadLine() == "no" ? true : false);
但是您对变量命名的选择非常模糊,可以使用更好的名称

bool stopLoop = false;
while (!stopLoop)
{
    .....
    stopLoop = (Console.ReadLine() == "no" ? true : false);
}
  int result;
  if(!Int32.TryParse(Console.ReadLine(), out result))
  {
       Console.WriteLine("Please enter a valid number");
       continue;
  }
正如下面的评论所指出的,如果没有更好的错误检查,您不应该尝试将用户键入的内容转换为整数。对非数值使用Convert.ToInt32会引发异常。更好的代码可能是

bool stopLoop = false;
while (!stopLoop)
{
    .....
    stopLoop = (Console.ReadLine() == "no" ? true : false);
}
  int result;
  if(!Int32.TryParse(Console.ReadLine(), out result))
  {
       Console.WriteLine("Please enter a valid number");
       continue;
  }

您可以使用
While
实现您想要的功能,只需按照不同的人的建议进行细微的更改,但您甚至可以使用
do While
而不是While,While将第一次进入循环,然后每次都会检查条件:-

正如MSDN所说:-

do语句执行一条语句或语句块 重复执行,直到指定表达式的计算结果为false。身体 除非它由一个 单一声明。在这种情况下,大括号是可选的


是否尝试更改条件(否==true)?提示:如果键入“是”而不是“否”,会发生什么情况?不回答,但如果希望循环至少运行一次,则应将其更改为Do..While循环。请使用tolower()和trim()然后放置断点以再次调试的值,并查看问题出在何处。您还可以了解break关键字。这样它就根本不会执行循环:p进行此更改不允许程序开始执行。@xtheking:不,程序开始执行,只是循环体不执行。但是是的,只需再次将
的初始值设置为
true
。这是使用
do…while
而不是
while
@MattBurland的最佳时机!再次=Console.ReadLine()=“否”;是的,在我的答案中添加了这一时刻。也许在你的答案中包括如果对其中一个数字添加了“是”将引发的异常?你仍然有与原始代码相同的问题<代码>!false==true
很抱歉,这是一个拼写错误@GrantWinneyAlso,使用了
Do…虽然对于这个特定循环来说可能更好一些,但这不是必需的。如果他们解决了条件的问题,原始循环就可以了。yes agree@MattBurland我刚刚给出了一个选项,因为其他答案已经给出了
bool again = false;
               do {
                Console.WriteLine("C# Basic Calcuator");
                Console.WriteLine("Please enter two numbers to be added: ");
                Console.WriteLine("Number 1: ");

                int result;
                int a = Convert.ToInt32(Console.ReadLine());


                Console.WriteLine("Number 2: ");
                int b = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Result: ");
                result = a + b;

                //print result
                Console.WriteLine(result);

                //potential exit condition
                Console.WriteLine("Would you like to calculate again?");
                if(Console.ReadLine() == "no")
                {
                    again = false;
                }
                else
                {
                    again = true;
                }

            }  while (again);