do-while循环中的try-catch块未正确循环,并且没有继承最后的输入结果

问题描述

Scanner scan = new Scanner(system.in);   
        int year = -1;
        int yearDuplicate = -1;
        System.out.println("This calculator tells you whether a year that you "
                + "enter is a leap year.\nPlease enter a year:");
        
        do{
            try{
                year = scan.nextInt();
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }    
            
        }while(year % 1 != 0);
            
        
        do{
            if(yearDuplicate % 4 == 0){ 
                if(yearDuplicate % 100 == 0){
                    if(yearDuplicate % 400 == 0){
                        System.out.println(yearDuplicate + " is a leap year.");
                    }else{
                        System.out.println(yearDuplicate + " is not a leap year.");
                    }
                }else{
                    System.out.println(yearDuplicate + " is a leap year.");
                }
            }else{
                System.out.println(yearDuplicate + " is not a leap year.");
            }
            
            System.out.println("Enter another year or enter '-1' to quit:");
            try{
                yearDuplicate = scan.nextInt();
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }
            
        }while(yearDuplicate != -1);

我正在尝试制作一个leap年计算器,该计算器还可以告诉您是不小心输入了字符串还是双精度而非整数。我希望该程序执行的操作是一遍又一遍地运行“错误。请输入不带小数的数字:”,直到用户输入整数为止。但是,当前,对于第一个try catch块,如果我输入了两种错误的输入类型,则会显示以下错误消息:

test
ERROR. Please enter a numerical digit without decimals:
-1 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
BUILD SUCCESSFUL

第二个至少好一点,它会不断重复我输入的最后一个整数的结果(年份“是a /不是a年”),但至少会重复一次,与第一个整数不同。

test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:

所有我想输入的错误消息都会被重播,直到您输入正确的数字为止。如何摆脱以前的输入结果,如何解决一个try catch块只能运行两次的事实?

我在网上看了看,但如果我更改了该程序的一小部分,似乎其他事情就会中断。如果我将第一个while循环的条件设置为while(year%1 == 0);但这意味着如果它是整数,则将重复while循环,这与我希望它执行的操作完全相反。如果我不满意scan.next();,它将给我一个无限的错误循环。

顺便说一句,有两个try catch块,因为用户在一年中有两次输入的机会,在两个地方我可能不得不更正它们。如果我将它们放在同一while循环中,则它将在第一次工作,但之后,每次需要输入两个数字时(第一个try捕获一个,第二个try捕获将覆盖一个)。 / p>

编辑:我将year和yearDuplicate的起始值更改为0,在try catch块中声明了扫描程序,并摆脱了scan.next();。在两个尝试捕获块。现在,我遇到了第一个try catch块的循环,但是它仍然保留了我以前的输入。

解决方法

您的基本“读取直到获得整数”块应如下所示:

    do {
        try {
            year = scan.nextInt();
            if (year <= 0) { // see comment
                System.out.println("ERROR. Please enter positive number");
            }
        } catch(Exception e){
            System.out.println("ERROR. Please enter a number "
                               + "without decimals");
            scan.next();
            year = -1;
        }                
    } while (year <= 0);

因此,如果存在非数字输入,则可以主动设置一个会导致循环的值,而不是依赖已经存在的值。

此外,您还要确保输入为正。

鉴于您应该能够弄清楚整个程序。

在我说“查看评论”的地方,也许您可​​能希望检查一个合理的年份;也许在1752年左右(英美采用格里高利历)之前就丢掉任何东西。

您最初的问题很可能是由于year % 1 != 0条件造成的。这意味着“除以1时的余数不为0”。对于非负值始终如此。在这种情况下,我必须查找Java如何实现负值的%

,

修正代码。

int year = 0;
        int yearDuplicate = 0;
        System.out.println("This calculator tells you whether a year that you "
                + "enter is a leap year.\nPlease enter a year:");
        
        do{
            Scanner scan = new Scanner(System.in);
            try{
                yearDuplicate = scan.nextInt();
                if(yearDuplicate % 4 == 0){ 
                    if(yearDuplicate % 100 == 0){
                        if(yearDuplicate % 400 == 0){
                            System.out.println(yearDuplicate + " is a leap year.");
                        }else{
                            System.out.println(yearDuplicate + " is not a leap year.");
                        }
                    }else{
                        System.out.println(yearDuplicate + " is a leap year.");
                    }
                }else{
                    System.out.println(yearDuplicate + " is not a leap year.");
                }
                System.out.println("Enter another year or enter '-1' to quit:");  
                
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }
            
        }while(yearDuplicate != -1);

基本上删除了第一个try catch块。使第二个try catch块包含整个do while循环。如果有人知道我以前犯的错误以及为什么犯错,我仍然想知道。