繁体   English   中英

do-while 循环中的 Try-catch 块未正确循环并保留最后的输入结果

[英]Try-catch blocks within do-while loops are not looping properly and carrying over the last input result

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

我正在尝试制作一个闰年计算器,它还会告诉您是否不小心输入了字符串或双精度而不是整数。 我希望这个程序做的是一遍又一遍地运行“错误。请输入一个没有小数的数字:”,直到用户输入一个整数。 但是,目前,对于第一个 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

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

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 catch 被第二个 try catch 覆盖)。

编辑:我将 year 和 yearDuplicate 的起始值更改为 0,在 try catch 块内声明扫描仪并摆脱了 scan.next(); 在两个 try catch 块中。 现在,我得到了一个包含第一个 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 循环。 如果有人知道我以前犯过哪些错误以及错误的原因,我仍然想知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM