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

问题描述 投票:-3回答:1

这是我班上的一个简单项目。用户只能输入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;
}
}

enter image description here

java if-statement error-handling while-loop
1个回答
0
投票

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

你还需要一个变量,如 int attempts 来跟踪尝试的次数。

按以下方式进行。

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;
    }
}

0
投票

很好的工作,用两种不同的变化进行尝试。问题的主要原因是 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.