C# while语句再次重复它

C# while语句再次重复它,c#,do-while,C#,Do While,除了while语句,一切都很好。如果你们能帮我,那就太好了。当用户写“Y”再次执行时,他看到:max value是:bla bla 用户必须给出一个新的正数,而不是反复查看最大值 Console.WriteLine("Please give a positieve number. if you enter a negatieve number its not going to work"); int invoer = 0, max = 0;

除了while语句,一切都很好。如果你们能帮我,那就太好了。当用户写“Y”再次执行时,他看到:max value是:bla bla

用户必须给出一个新的正数,而不是反复查看最大值

Console.WriteLine("Please give a positieve number. if you enter a negatieve number its not going to work");


                int invoer = 0, max = 0;
                string repeat = "";


                do
                {

                    for (int i = 1; invoer >= 0; i++)
                    {

                        Console.Write(i + "> ");
                        invoer = int.Parse(Console.ReadLine());

                        if (max < invoer)
                        {
                            max = invoer;
                        }
                    }


                    Console.WriteLine("Maximum value is: " + max);


                    Console.WriteLine("do you want to try again? y/n: ");
                    repeat = Console.ReadLine();

                } while (repeat == "y" || repeat == "Y");
Console.WriteLine(“请给出一个正数。如果输入一个负数,它将不起作用”);
整数发票=0,最大值=0;
字符串repeat=“”;
做
{
对于(int i=1;发票>=0;i++)
{
控制台。写入(i+“>”);
invore=int.Parse(Console.ReadLine());
如果(最大值<发票人)
{
最大值=发票人;
}
}
Console.WriteLine(“最大值为:”+max);
Console.WriteLine(“您想再试一次吗?是/否:”);
repeat=Console.ReadLine();
}while(repeat==“y”| repeat==“y”);

我不能100%确定您想做什么,但看起来您需要将invover和max的减速移动到do循环中。

我在猜测这应该做什么

   {

                //Console.WriteLine("Please give a positive number. if you enter a negative number its not going to work");

                int invoer;
                int max;
                string repeat;



                do
                {
                    //they have given a negative number and wish to try again
                    Console.WriteLine("Please give a positive number.\nIf you enter a negative number its not going to work");

                    invoer = 0;
                    max = 0;
                    repeat = "";

                    for (int i = 1; invoer >= 0; i++)
                    {

                        Console.Write(i + "> ");
                        invoer = int.Parse(Console.ReadLine());

                        if (max < invoer)
                        {
                            max = invoer;
                        }
                    }


                    Console.WriteLine("Maximum value is: " + max);


                    Console.WriteLine("do you want to try again? y/n: ");
                    repeat = Console.ReadLine();

                } while (repeat == "y" || repeat == "Y");


            }
{
//WriteLine(“请给出一个正数。如果输入负数,它将不起作用”);
国际发票人;
int max;
字符串重复;
做
{
//他们给出了一个负数,希望再试一次
Console.WriteLine(“请给出一个正数。\n如果输入负数,它将不起作用”);
发票=0;
max=0;
重复“”;
对于(int i=1;发票>=0;i++)
{
控制台。写入(i+“>”);
invore=int.Parse(Console.ReadLine());
如果(最大值<发票人)
{
最大值=发票人;
}
}
Console.WriteLine(“最大值为:”+max);
Console.WriteLine(“您想再试一次吗?是/否:”);
repeat=Console.ReadLine();
}while(repeat==“y”| repeat==“y”);
}

好的,您更正了错误。问题解决了吗?没有。问题没有解决。我只是忘了把它翻译成英语哈,好吧。在for循环之前设置max=0和invoer=0,否则它们仍然具有上一次运行的值。您太棒了。问题解决了。但是你能解释为什么吗?max和invoer是变量,它们存储值。如果他们忘记了价值观,那就没什么用了,对吧?