首页 > 解决方案 > 为什么 Java 'break' 语句不起作用?

问题描述

这是我的代码:

public static void main(String args[]) throws Exception {
    
    int length; 
    Scanner input = new Scanner (System.in);
    Scanner scanner = new Scanner(System.in);
    
    System.out.println(" How many dates would you like to include for the expiration dates notification? ");
    length=scanner.nextInt();  
    String[] dates = new String[length]; 
    System.out.println(" input date, formatting is hh:mm:ss");
    for(int i=0; i<length; i++)  
    {
    dates[i]=scanner.next(); 
    }
    
    System.out.println("== DATE LIST =="); 
    for (int i=0; i<length; i++)   
    {  
    System.out.println(dates[i]); 
    }  
    
    int loopcount;
            System.out.print("Please enter how much notification you would like to put in: ");
            loopcount = input.nextInt();
            
    
    System.out.println("== EXPIRE LIST ==\n"); 
    while (true) { 
        for (String z : dates) { 
            Thread.sleep(50); 

            LocalDateTime now = LocalDateTime.now();
            Date convertednow = Date.from(now.atZone(ZoneId.systemDefault()).toInstant()); 
            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); 
            String strnow = dateFormat.format(convertednow); 

            if (strnow.equals(z)) { 
                System.out.print(z+" has expired\n"); 
               
                
                
            
            int i = 0;                
            while(i < loopcount) { 
                System.out.println("Notification stopped");
                i++;
                if (i == 3) {
                    break;
                }
                
            }
            
            
            
        }
    }
   }
}

我是 Java 新手。对不起,如果我犯了很多错误。我试图做的是制作一个计时器,当到达特定时间时通知用户。用户将通过扫描仪输入计时器。我的计划是当我之前输入的所有计时器都得到通知时,它将停止并在输出的最后一行给出消息“通知停止”。我现在面临的问题是每当我试图通过使用 break 语句来停止循环时。它只是不起作用,它会一直卡在无限循环中,直到我强制它停止。

谢谢您阅读此篇。

标签: javaloopswhile-loop

解决方案


break语句退出最内层循环,即while(i < loopcount)循环,而不是中间for (String z : dates)循环或外层while (true)循环。

如果要break退出外循环,可以使用标签来完成:

System.out.println("== EXPIRE LIST ==\n");
MAIN: while (true) { // <== Added label 'MAIN'
    for (String z : dates) {
        Thread.sleep(50);

        . . .

        int i = 0;
        while(i < loopcount) {
            System.out.println("Notification stopped");
            i++;
            if (i == 3) {
                break MAIN; // <== Exit loop labeled 'MAIN'
            }
        }
    }
}

注意:有些人强烈反对使用标签,因为它的行为类似于goto其他语言中的语句,并且被认为对代码结构不利。

如果循环return后没有代码,您也可以使用语句。while (true)反对这一点的论点有些相同(代码结构)。

另一种方法是使用boolean变量。

System.out.println("== EXPIRE LIST ==\n");
boolean done = false;
while (! done) { // <== Exit outer loop when "done"
    for (String z : dates) {
        Thread.sleep(50);

        . . .

        int i = 0;
        while(i < loopcount) {
            System.out.println("Notification stopped");
            i++;
            if (i == 3) {
                done = true;
                break; // <== Exit inner while-loop
            }
        }
        if (done)
            break; // <== Exit for-loop
    }
}

推荐阅读