输入错误后,继续类之间的循环

本文关键字:之间 循环 继续 错误 输入 | 更新日期: 2023-09-27 18:16:42

我正在尝试创建一个类,该类将从控制台窗口收集的一些整数求和。该类首先请求要和多少个数字,然后从控制台中读取这些整数。

如果用户输入一个带有小数的数字(这是不允许的),我有另一个类Input.cs,它会返回一个消息,要求用户再试一次。这个类有4个方法,WriteProgamInfo, ReadInput, SumNumbers和ShowResults。WriteProgamInfo只打印一些带有Console.WriteLine的信息,ReadInput询问用户想要添加多少个数字,

private void ReadInput()
{
    Console.Write("Number of values to sum? ");
    numOfInput = Input.ReadIntegerConsole(); //This here is from the Input class that sends an error message if a decimal is being used. This works fine.
    Console.WriteLine();
}

但是现在我有一个问题,在SumNumbers中,它要求用户使用for循环给出他/她想要添加的数字的值,该循环依赖于用户在ReadInput中输入的内容。

// asks for which numbers to be added, based on how many times
// is given by user in ReadInput
private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) // Loop that asks the quesion d
    {
        Console.Write("Please give the value no. " + index + " (whole number):");
        //This here is from the Input class that sends an error message if a decimal
        //is being used. This is where I have a problem. 
        num = Input.ReadIntegerConsole2();
        sum += num;
    }
}

我遇到的问题是,我希望错误消息在发送消息后继续for循环。例如,我想求和两个数字,所以我先加1,然后控制台打印:请给出值2(整数):但是,假设我然后输入1.6,这是不允许的,控制台然后打印:错误输入。请再试一次:,现在我想在这里继续和之前一样的问题:"请给出值2(整数):"

我该怎么做?输入类看起来像这样:

public static int ReadIntegerConsole2()
{
    int input;
    if (int.TryParse(Console.ReadLine(), out input))
        return input;
    else
        Console.WriteLine("Wrong input. Please try again: ");
    Console.Write("Please give the value no. " + /*index from SumNumbers + */" (whole number):");
    return ReadIntegerConsole2();
}

输入错误后,继续类之间的循环

您可以使用Int32.TryParse使用的相同模式。这意味着您将要添加的数字作为输出参数传递,如果数字无效则返回布尔值false

public static bool ReadIntegerConsole2(out int number)
{
    int input;
    bool ok = true;
    number = 0;
    if (int.TryParse(Console.ReadLine(), out input))
        number = input;
    else
    {
        ok = false;
        Console.WriteLine("Wrong input. Please try again: ");
    }
    return ok;
}

现在在调用代码中,你可以控制循环,知道调用ReadInteger2

的结果
private void SumNumbers() 
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) 
    {
        Console.Write("Please give the value no. " + index + " (whole number):");
        if(Input.ReadIntegerConsole2(out num))
            sum += num;
        else 
            // Decrement the counter to avoid skipping an input
            index--;
    }
}

有很多可能的方法来实现你的目标。但在我看来,最明显的方法之一是遵循您已经在使用的int.TryParse()设置的示例。改变你的ReadIntegerConsole2()方法以同样的方式工作:

public static bool TryReadIntegerConsole2(out int input)
{
    if (int.TryParse(Console.ReadLine(), out input))
        return true;
    Console.WriteLine("Wrong input. Please try again: ");
    return false;
}

然后像这样在循环中使用它:

private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) // Loop that asks the quesion d
    {
        do
        {
            Console.Write("Please give the value no. " + index + " (whole number):");
        } while (!Input.TryReadIntegerConsole2(out num));
        sum += num;
    }
}

当您试图检索第二个值时,继续循环,直到检索到的值对您的情况有效。

private int GetUserValue(int index)
{
    var secondValueValid = false;
    int secondValue;
    do
    {
        Console.WriteLine("Please enter the value at index {0}: ", index);
        if(int.TryParse(Console.ReadLine(), out secondValue))
        {
            secondValueValid = true;
        }
        else
        {
            Console.WriteLine("Value entered is not a whole integer, please try again.");
        }
    }
    while(!secondValueValid)
    return secondValue;
}

这将循环,直到用户输入一个整数值,在这种情况下,它将退出循环并返回输入的值,在您的SumNumbers方法中,您需要做的就是:

private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++)
    {
        num = GetUserValue(index);
        sum += num;
    }
}