Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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#_While Loop - Fatal编程技术网

C#无限循环。如何应用循环控制变量?

C#无限循环。如何应用循环控制变量?,c#,while-loop,C#,While Loop,正在尝试分配任务。我知道我需要添加一个循环控制变量,但我不确定该怎么做。任何指点都将不胜感激 namespace QueenslandRevenue { class Program { static void Main(string[] args) { Console.WriteLine("Number of contestants last year?>> "); int contestLast

正在尝试分配任务。我知道我需要添加一个循环控制变量,但我不确定该怎么做。任何指点都将不胜感激

namespace QueenslandRevenue
{
class Program
{
    static void Main(string[] args)
    {
        

        Console.WriteLine("Number of contestants last year?>> ");
        int contestLast = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Number of contestatnts this year?>> ");
        int contestThis = Convert.ToInt32(Console.ReadLine());


        Console.WriteLine(contestThis + contestLast);
        Console.ReadLine();
const int MAX=30;
常数int MIN=0;
while(竞赛last>MAX | |竞赛lastMAX | |竞赛this
静态void Main(string[]args)
方法中,添加一个while循环,该循环将一直运行,直到用户输入有效的输入。当他这样做时,您将布尔变量
stop
更改为true,这样执行将中断循环

int MAX=30;
int MIN=0;
bool-stop=false;
当(!停止)
{
Console.WriteLine(“去年的参赛人数?>>”;
int contestLast=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“今年的竞赛数量?>>”;
int=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
如果(竞赛last>MAX | |竞赛lastMAX | |竞赛this
静态void Main(string[]args)
方法中,添加一个while循环,该循环将一直运行,直到用户输入有效的输入。当他这样做时,您将布尔变量
stop
更改为true,这样执行将中断循环

int MAX=30;
int MIN=0;
bool-stop=false;
当(!停止)
{
Console.WriteLine(“去年的参赛人数?>>”;
int contestLast=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“今年的竞赛数量?>>”;
int=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
如果(竞赛last>MAX | |竞赛lastMAX | |竞赛this
首先,我建议提取一个方法(我们不应该重复):

首先,我建议提取一种方法(我们不应该重复):


如果用户正确地输入了
competinglast
,但错误地输入了
competingthis
,它将循环直到用户为
competingthis
指定了正确的值,而不是重新开始整个过程

    int contestLast = ReadInteger("Number of contestants last year?>> ", 1, 30);
    int contestThis = ReadInteger("Number of contestants this year?>> ", 1, 30);
static void Main(字符串[]args)
{
最后,这个;
控制台。写(“去年参赛者人数?>>”;
而(!int.TryParse(Console.ReadLine(),out contestLast)| |(contestLast<0)| |(contestLast>30))
{
Write(“指定的值不是数字或不在[0,30]范围内,请重试>>”;
}
控制台。写入(“今年的竞赛数量?>>”;
而(!int.TryParse(Console.ReadLine(),out contestThis)| |(contestThis<0)| |(contestThis>30))
{
Write(“指定的值不是数字或不在[0,30]范围内,请重试>>”;
}
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
}
您也可以这样编写,如果您还需要其他任何内容的循环:

static void Main(string[] args)
{
    int contestLast, contestThis;

    Console.Write("Number of contestants last year?>> ");

    while (!int.TryParse(Console.ReadLine(), out contestLast) || (contestLast < 0) || (contestLast > 30))
    {
        Console.Write("The specified value was either not a number or was not in range [0, 30], please try again>> ");
    }
    Console.Write("Number of contestatnts this year?>> ");

    while (!int.TryParse(Console.ReadLine(), out contestThis) || (contestThis < 0) || (contestThis > 30))
    {
        Console.Write("The specified value was either not a number or was not in range [0, 30], please try again>> ");
    }
    Console.WriteLine(contestThis + contestLast);

    Console.ReadLine();
}
private static int ReadNumber(字符串消息,int-min,int-max)
{
int结果;
控制台。写入(消息);
而(!int.TryParse(Console.ReadLine(),out result)| |(resultmax))
{
Write($“指定的值不是数字或不在范围[{min},{max}]内,请重试>>”;
}
返回结果;
}
静态void Main(字符串[]参数)
{
int contesticlast=ReadNumber(“去年的参赛人数?>>”,0,30);
int contestics=ReadNumber(“今年的参赛人数?>>”,0,30);
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
}

如果用户正确输入了
contestLast
,但输入了
contestThis
不正确,它将循环,直到用户为
contestThis
指定了正确的值,而不是重新开始整个过程

    int contestLast = ReadInteger("Number of contestants last year?>> ", 1, 30);
    int contestThis = ReadInteger("Number of contestants this year?>> ", 1, 30);
static void Main(字符串[]args)
{
最后,这个;
控制台。写(“去年参赛者人数?>>”;
而(!int.TryParse(Console.ReadLine(),out contestLast)| |(contestLast<0)| |(contestLast>30))
{
Write(“指定的值不是数字或不在[0,30]范围内,请重试>>”;
}
控制台。写入(“今年的竞赛数量?>>”;
而(!int.TryParse(Console.ReadLine(),out contestThis)| |(contestThis<0)| |(contestThis>30))
{
Write(“指定的值不是数字或不在[0,30]范围内,请重试>>”;
}
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
}
您也可以这样编写,如果您还需要其他任何内容的循环:

static void Main(string[] args)
{
    int contestLast, contestThis;

    Console.Write("Number of contestants last year?>> ");

    while (!int.TryParse(Console.ReadLine(), out contestLast) || (contestLast < 0) || (contestLast > 30))
    {
        Console.Write("The specified value was either not a number or was not in range [0, 30], please try again>> ");
    }
    Console.Write("Number of contestatnts this year?>> ");

    while (!int.TryParse(Console.ReadLine(), out contestThis) || (contestThis < 0) || (contestThis > 30))
    {
        Console.Write("The specified value was either not a number or was not in range [0, 30], please try again>> ");
    }
    Console.WriteLine(contestThis + contestLast);

    Console.ReadLine();
}
private static int ReadNumber(字符串消息,int-min,int-max)
{
int结果;
控制台。写入(消息);
而(!int.TryParse(Console.ReadLine(),out result)| |(resultmax))
{
Write($“指定的值不是数字或不在范围[{min},{max}]内,请重试>>”;
}
返回结果;
}
静态void Main(字符串[]参数)
{
int contesticlast=ReadNumber(“去年的参赛人数?>>”,0,30);
int contestics=ReadNumber(“今年的参赛人数?>>”,0,30);
Console.WriteLine(竞赛本次+竞赛末次);
Console.ReadLine();
}

在主功能中,您可以尝试
private static int ReadNumber(string message, int min, int max)
{
    int result;

    Console.Write(message);

    while (!int.TryParse(Console.ReadLine(), out result) || (result < min) || (result > max))
    {
        Console.Write($"The specified value was either not a number or was not in range [{min}, {max}], please try again>> ");
    }
    return result;

}

static void Main(string[] args)
{
    int contestLast = ReadNumber("Number of contestants last year?>> ", 0, 30);
    
    int contestThis = ReadNumber("Number of contestants this year?>> ", 0, 30);

    Console.WriteLine(contestThis + contestLast);

    Console.ReadLine();
}
int contestLast = 0;
int contestThis = 0;
const int MAX = 30;
const int MIN = 0;

do {
    Console.WriteLine("Number of contestants last year? ");
    contestLast = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Number of contestants this year? ");
    contestThis = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(contestThis + contestLast);
    if (contestLast > MAX || contestLast < MIN || contestThis > MAX || contestThis < MIN)
        Console.WriteLine("Please enter valid number of contestants");
} while (contestLast > MAX || contestLast < MIN || contestThis > MAX || contestThis < MIN);

Console.WriteLine("Finish");