Java 首次使用try、catch和标签

Java 首次使用try、catch和标签,java,label,try-catch,break,continue,Java,Label,Try Catch,Break,Continue,我们刚刚在昨晚的讲座中谈到了“尝试捕捉”的话题。这个作业不需要写,但我想我会尝试一下 我已经为此挣扎了一段时间。使用continue语句将其踢回catch块的开头,以便其中的println在一个永无止境的循环中执行 很明显,有一些东西叫做标签。我发现,当我试图找出如何解决这个问题。正如我在示例代码中看到的那样,我尝试添加一个标签,但现在它在break语句中给了我一个编译器错误,表示缺少进程标志 我做错了什么 do { // Prompt try

我们刚刚在昨晚的讲座中谈到了“尝试捕捉”的话题。这个作业不需要写,但我想我会尝试一下

我已经为此挣扎了一段时间。使用continue语句将其踢回catch块的开头,以便其中的println在一个永无止境的循环中执行

很明显,有一些东西叫做标签。我发现,当我试图找出如何解决这个问题。正如我在示例代码中看到的那样,我尝试添加一个标签,但现在它在break语句中给了我一个编译器错误,表示缺少进程标志

我做错了什么

    do {
        // Prompt
        
    try {
       process: nbPlayers = keyboard.nextInt(); 
        
        }
        catch(Exception e) {

            nbPlayers=0;    
            if (attempts<4) {
                System.out.println("Incorrect input type. You have now made " + attempts +". Please enter an integer from 2 to 4.");
                ++attempts;
                break process;              

            }
            else {
                System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\n Shutting down the program now.");
                System.exit(0);
            }
            
        }
    
        if(attempts < 4 && nbPlayers >= 2 && nbPlayers <= 4) {
            valid = true;
        } else if(attempts < 3) {
            System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\nYou are allowed to try again.");
        } else if(attempts == 3) {
            System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\nYou are allowed to one more time.");
        } else if(attempts == 4) {
            System.out.println("Invalid input. You have now made " + attempts +". This game only allows 4 chances to input the requested number of players.\n Shutting down the program now.");
            System.exit(0);
        }

        ++attempts;
        
    } while(!valid);

你的问题不太清楚。让我们谈谈代码。 你觉得这个怎么样

 int attempts = 0; // is it a good initialization ?
 int nbPlayers = 0; // is it a good initialization ?
 boolean valid = false; // is it a good initialization ?
 String nbPlayersString = "";
 do {
        // Prompt
        
    try {
           nbPlayersString = keyboard.nextLine(); 
           nbPlayers = Integer.parseInt(nbPlayersString);
           if(2<= nbPlayers  && nbPlayers <= 4) {
               valid = true;
           }
           else {
               System.out.println("Bad Attempt " + (attempts + 1) +". Invalid number of players.");
           }
        }
        catch(Exception e) {
            nbPlayers=0; 
            System.out.println("Bad Attempt " + (attempts + 1) +". Wrong input type. ");
  
        }
    
        

        ++attempts;
        
    } while(!valid && attempts < 4);
 
 if(valid)
     System.out.println("GOOD JOB!");
 else {
     System.out.println("You have exhausted all your chances. Program will terminate!");
     System.exit(0);
 }
最终编辑
在输入无效值字符串而不是整数后,Scanner.nextInt似乎也有缓存问题。因此,在下面的尝试中,扫描仪缓存仍然有错误的值并考虑了它。

您是否将process:放在//提示符处?这是第四行:process:nbPlayers=keyboard.nextInt;对不起,我的意思是把它放在do{之前,这样它就会变成process:do{除非您绝对需要使用标签,否则我会避免使用标签。标签会导致创建非常难以维护的代码。事实上,仔细阅读您的问题,标签不应该以这种方式使用。标签最适合终止循环。我建议您使用boolean process=true,然后逐个process=fal转换break process在最后一个if.Ex:ifprocess之前插入一个if{….ifattempts<4&&nbPlayers>=2&&nbPlayers这是一个复制粘贴问题,输入类型部分错误。我刚刚更新。告诉我更多有关您输入的值现在您认为如何?现在更新版本?键盘的类型是什么?是扫描仪吗?如果是,请尝试。下一行,然后现在再次解析输入测试。。。