提问者:小点点

设置在Java中运行while循环的限制


这是我班上的一个简单项目。 用户只能错误输入3次,如果他输入超过3次,程序应该终止。 我不知道该用什么循环。 请提供一些想法。 我尝试使用FOR循环,但它不工作

public class Main {

public static void main(String[] args) {

    int numl = readNumber("number1", 1, 10);
    int num2 = readNumber("number2", 11, 20);
    int add = Total(numl + num2);
}

public static int readNumber(String Prompt, int min, int max) {
    int value ;
    Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print(Prompt);
            value = scanner.nextInt();
            if ((value >= min && value <= max)) {
                break;
            }
        }
    return value;
}
public static int Total(int sum) {
    System.out.println("SUM = " + sum);
    return sum;
}
}


共2个答案

匿名用户

我建议您使用do-while循环(这并不意味着您不能使用其他类型的循环来解决它),因为这将使代码易于理解。 do-while循环保证其主体将至少执行一次。

您还需要一个变量,例如inttempts来跟踪尝试的次数。

按以下步骤执行:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int numl = readNumber("Number1: ", 1, 10);
        int num2 = readNumber("Number2: ", 11, 20);
        int add = Total(numl + num2);
    }

    public static int readNumber(String Prompt, int min, int max) {
        int value;
        int attempts = 0;
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.print(Prompt);
            value = scanner.nextInt();
            if ((value >= min && value <= max)) {
                break;
            }
            attempts++;
        } while (attempts < 3);

        if (attempts == 3) {
            // Reset `value`
            value = 0;
        }

        return value;
    }

    public static int Total(int sum) {
        System.out.println("SUM = " + sum);
        return sum;
    }
}

运行示例:

Number1: 10
Number2: 15
SUM = 25

另一个示例运行:

Number1: 50
Number1: 56
Number1: -1
Number2: 23
Number2: -60
Number2: 50
SUM = 0

下面给出了使用while循环而不是do-while循环的代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int numl = readNumber("Number1: ", 1, 10);
        int num2 = readNumber("Number2: ", 11, 20);
        int add = Total(numl + num2);
    }

    public static int readNumber(String Prompt, int min, int max) {
        int value;
        int attempts = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (attempts == 3) {
                // Reset `value`
                value = 0;
                break;
            }
            System.out.print(Prompt);
            value = scanner.nextInt();
            if ((value >= min && value <= max)) {
                break;
            }
            attempts++;
        }

        return value;
    }

    public static int Total(int sum) {
        System.out.println("SUM = " + sum);
        return sum;
    }
}

匿名用户

很好的工作,使两个不同的变化尝试。 问题的主要原因是while(true)段。 您要做的是在每次迭代时都要检查的段中放置一个条件。 既然您想检查尝试的数量是否小于某个最大值,那么您就需要检查一下。

注意,在我所做的修改中,如果while循环段中的条件在任何变体中都没有得到满足,就会抛出IllegalStateException,这表明没有找到结果。

public class Main {

    public static void main(String[] args) {
        try {
            int numl = readNumber("number1", 1, 10, 3);

            int num2 = readNumber("number2", 11, 20, 3);

            int add = Total(numl + num2);
        } catch (IllegalStateException ise) {
            // tell user they exceeded N attempts in 1 of the 2 numbers

        }
}
public static int readNumber(String Prompt, int min, int max, int availableAttempts) {
    int value;

    int attempts = 0;

    Scanner scanner = new Scanner(System.in);
    while (attempts++ < availableAttempts) {
        System.out.print(Prompt);

        value = scanner.nextInt();

        if ((value >= min && value <= max)) {
            return value;
        }
    }
    throw new IllegalStateException();
}

public static int Total(int sum) {
    System.out.println("SUM = " + sum);

    return sum;
}