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

时间:2015-10-09 21:42:15

标签: java string while-loop println

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

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

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

任何提示/技巧或其他特殊方式来解决这个问题?

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

2 个答案:

答案 0 :(得分:0)

请尝试这个,只需将所有代码包装在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());
}

答案 1 :(得分:0)

您没有消费用户输入扫描仪缓冲区的内容,这就是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();
    }