如何防止字符串在while循环中重复打印?

How to prevent string from printing repeatedly in while loop?

我目前正在做老师给我的一些练习。这些是为假期准备的,所以我无法在那里寻求帮助。

我有这段代码,它根据用户定义的整数创建乘法 table,范围从用户定义的最小值和最大值。

在将我的任何变量设置为扫描仪中的下一个整数之前,我会检查扫描仪是否确实有整数。这工作正常,但我不希望它打印出十亿次错误消息。

任何 tips/tricks 或其他特殊方法来解决这个问题?

public class MultiplicationTable
{
    private int intervalMin;
    private int intervalMax;
    private int multiplier;
    private int result;
    private Scanner sc;

    public MultiplicationTable()
    {
        multiplier = 0;
        intervalMin = 0;

        sc = new Scanner(System.in);

        while (multiplier == 0)
        {
            System.out.println("Please enter the integer you wish to show the table for");
            if (sc.hasNextInt())
            {
                multiplier = sc.nextInt();

            }
            else
            {
                System.out.println("Input is not an integer\n");
            }
        }

        while (intervalMin == 0)
        {
            System.out.println("\nPlease enter the integer defining the start of the table");
            if (sc.hasNextInt())
            {
                intervalMin = sc.nextInt();
            }
            else
            {
                System.out.println("Input is 0 or not an integer\n");
            }

        }

        while (intervalMax == 0)
        {
            System.out.println("\nPlease enter the integer defining the end of the table");
            if (sc.hasNextInt())
            {
                int i = sc.nextInt();
                if (i > intervalMin)
                {
                    intervalMax = i;
                }
                else
                {
                    System.out.println("\nEnd integer must be greater than start integer");
                }
            }
            else
            {
                System.out.println("Input is 0 or not an integer");
            }

        }

        System.out.println("\nTable for integer " + multiplier + " from " + intervalMin + " to " + intervalMax + "\n");
        for (int i = intervalMin; i <= intervalMax; i++)
        {
            result = i * multiplier;
            System.out.println(i + " * " + multiplier + " = " + result);
        }
    }
}

请试试这个,只需将所有代码包装在 try catch 中:

try {
    while (intervalMax == 0) {
        System . out . println("\nPlease enter the integer defining the end of the table");
        if (sc . hasNextInt()) {

            int i = sc . nextInt();

            if (i > intervalMin) {
                intervalMax = i;
            } else {
                throw new Exception("\nEnd integer must be greater than start integer");
            }
         }

    }
} catch (Exception e) {
    System . out . println(e . getMessage());
}

您没有消耗用户输入扫描仪缓冲区的内容,这就是为什么 sc.hasNextInt() 一直执行而不等待下一个用户输入。 解决方法是在if条件后面加上sc.nextLine()。 例如:

boolean gotInteger = false;
    while (!gotInteger) {
        System.out.println("Please enter the integer you wish to show the table for");
        if (sc.hasNextInt()) {
            multiplier = sc.nextInt();
            gotInteger = true;
        } else {
            System.out.println("Input is not an integer\n");
        }
        sc.nextLine();
    }