try catch后尝试重启时的无限循环

时间:2014-05-07 05:37:57

标签: java

[已解决]通过添加console.next();我的捕获声明。

catch(InputMismatchException fe) {
    System.out.println("You didnt enter an integer. Try again");
    restart = 'y';
    console.next();
}}

while(restart=='y');
}}

[解决] ^^^^^^

static public char restart;

public static void main(String[] args) {
    do {
        try {
            int num1, num2, check, sum = 0, count = 1, sqsum = 0;
            do {
                System.out.println("Please enter a number");
                num1 = console.nextInt();

这里是我将重启设置为公共的所以我可以将它与try catch一起使用,以便在inputmissmatchexception发生时重新启动程序

catch(InputMismatchException fe) {
    System.out.println("You didnt enter an integer. Try again");
    restart = 'y';
}}

while(restart=='y');
}}

并且这里是捕获,如果它确实捕获异常我将重新启动设置为y然后如果它是y然后do while将重新启动程序。但它并不是要求用户输入一个数字然后显示我的捕获消息"你没有输入整数....."我相信这意味着扫描仪不会在变量中搜索另一个值,但我已经尝试过并且不知道如何解决这个问题。

FULL:这是一个学校的项目,我必须让他们输入两个数字并为他们做事。它工作并完成它应该做的一切,但我只是想让程序重新启动,如果用户输入一个非整数。

package p321ex9;

import java.util.*;

public class p321ex9 {
    static Scanner console = new Scanner(System.in);
    static public char restart = 'n';

    public static void main(String[] args) {
        do {
            try {
                int num1, num2, check, sum = 0, count = 1, sqsum = 0;
                do {
                    System.out.println("Please enter a number");

                    num1 = console.nextInt();

                    System.out.println("Please enter a second number that is grester than the first number you entered which "
                            + "was " + num1 + ".");

                    num2 = console.nextInt();
                    if (num1 > num2) {
                        System.out.println("the first number was greater than your second number, please enter new numbers and "
                                + "make sure the second is greater.");
                    }
                }
                while (num1 > num2);
                System.out.println("List of odd numbers between the first and second numbers you entered inclusive:");
                while (num1 <= num2) {
                    check = (num1 % 2);
                    if (check == 1) {
                        sqsum += (num1 * num1);
                        System.out.print(num1 + " ");
                    } else {
                        sum += num1;
                    }
                    num1++;
                }
                System.out.println();
                System.out.println("The sum of all even numbers the first and second numbers you entered inclusive"
                        + ": " + sum);
                System.out.println("Sum of the squares of all the odd numbers between your two entered numbers inclusive: " + sqsum);
                System.out.println("All numbers from 1 to 10 squared");
                while (count <= 10) {
                    System.out.print(count + " squared = " + (count * count) + "   ");
                    count++;
                }
            } catch (InputMismatchException fe) {
                System.out.println("You didnt enter an integer. Try again");
                restart = 'y';
            }
        }
        while (restart == 'y');

    }
}

1 个答案:

答案 0 :(得分:1)

因为当你设置restart='y'时,当用户输入有效输入(整数)时你没有重置它

更好的方法:

public static void main (String[] args){

    Scanner console=new Scanner(System.in);
    int num1,num2,check,sum=0,count=1,sqsum=0;
    do{
        System.out.println("Please enter a number");
        String input=console.nextLine();

        if(input.replaceAll("\\d","").length()==0){
            num=Integer.parseInt(input);
            restart='n';
        }
        else{
            System.out.println("You didnt enter an integer. Try again");
            restart='y';
        }
    }
    while (restart=='y');
}