繁体   English   中英

无限循环if语句

[英]Infinitely Looping if statement

首先:我在以下代码中不断陷入无限循环。 构建代码的目的是,您输入1到10之间的8个数字。这证明这些数字在1到10之间。然后第二个证明是将这8个数字加在一起等于60。否则,我会遇到麻烦声明。 如果总数为60,则可以正常工作并进行打印,但如果在if语句处停下来,它将无限打印“您输入了错误的总数”打印输出,并且总积分不断增加。 我是否错过了一些容易的内容,例如括号或其他内容?

其次:如果总数不为60,如何返回到原始值输入并重新开始整个过程​​?

public static void lebronJames() {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);

     //Declare an array to hold 8 intgers values
     int lebronJamesAttributes[] = new int[8];
     int attribute = 0;


     System.out.println("Please allocate your attribute points for Lebron James in the following order. Your point allocations per attribute should be between 1 and 10. You have a total of 60 points to allocate");
     System.out.println("-----------------");
     System.out.println("Close Range" + "\n" + "Mid Range" + "\n" + "Three Point" + "\n" + "Free Throw" + "\n" + "Offensive Rebound" + "\n" + "Defensive Rebound" + "\n" + "Assist" + "\n" + "Steal" + "\n");        

     while (attribute <= 7) {
         int attributeValue = input.nextInt();
         if (attributeValue >= 1 && attributeValue <= 10 ) {
             lebronJamesAttributes[attribute] = attributeValue;
             attribute++;
         } 
         else {
             System.out.println("The attribute value you have selected is out of range. Select again.");
         }  
     }              
            int jamesTotalQuarter = 0;
            while (jamesTotalQuarter != 60){
                for (int jamesTotalQ1 : lebronJamesAttributes){
                     jamesTotalQuarter += jamesTotalQ1;
                }


                if (jamesTotalQuarter != 60) {
                    System.out.println("You have entered the wrong total number of attribute points. Please enter a total of 60 attribute points between the 8 characteristics.");
                    System.out.println("You have allocated a total of " + jamesTotalQuarter + " points.");
                }

                else {
                    System.out.println("Close Range" + lebronJamesAttributes[0] + "\n" + "Mid Range" + lebronJamesAttributes[1] + "\n" + "Three Point" + lebronJamesAttributes[2] + "\n" + "Free Throw" + lebronJamesAttributes[3] + "\n" + "Offensive Rebound" + lebronJamesAttributes[4] + "\n" + "Defensive Rebound" + lebronJamesAttributes[5] + "\n" + "Assist" + lebronJamesAttributes[6] + "\n" + "Steal" + lebronJamesAttributes[7] + "\n");         
                    System.out.println("You have allocated a total of " + jamesTotalQuarter + " points.");}

                }


                }
}

让我知道以下代码是否可以解决您的问题。 还没有测试过,但是现在应该可以正常工作了。 获取逻辑并自己修复,以防出现错误。

public static void lebronJames() {
                // TODO Auto-generated method stub
                 Scanner input = new Scanner(System.in);
               While(true){ // keeps it in a loop forever and only a "break" statement can stop it

                 //Declare an array to hold 8 intgers values
                 int lebronJamesAttributes[] = new int[8];
                 int attribute = 0;
                 System.out.println("Please allocate your attribute points for Lebron James in the following order. Your point allocations per attribute should be between 1 and 10. You have a total of 60 points to allocate");
                 System.out.println("-----------------");
                 System.out.println("Close Range" + "\n" + "Mid Range" + "\n" + "Three Point" + "\n" + "Free Throw" + "\n" + "Offensive Rebound" + "\n" + "Defensive Rebound" + "\n" + "Assist" + "\n" + "Steal" + "\n");        

                 while (attribute <= 7) {
                     int attributeValue = input.nextInt();
                     if (attributeValue >= 1 && attributeValue <= 10 ) {
                         lebronJamesAttributes[attribute] = attributeValue;
                         attribute++;
                     } 
                     else {
                         System.out.println("The attribute value you have selected is out of range. Select again.");
                     }  
                 }              
                 int jamesTotalQuarter = 0;
                 for (int jamesTotalQ1 : lebronJamesAttributes){
                      jamesTotalQuarter += jamesTotalQ1;
                 }


                 if (jamesTotalQuarter != 60) {
                      System.out.println("You have entered the wrong total number of attribute points. Please enter a total of 60 attribute points between the 8 characteristics.");
                      System.out.println("You have allocated a total of " + jamesTotalQuarter + " points.");
                     continue;     // loop execution starts from the beginning and your program gets fresh inputs.       
    }
                 else {
                                System.out.println("Close Range" + lebronJamesAttributes[0] + "\n" + "Mid Range" + lebronJamesAttributes[1] + "\n" + "Three Point" + lebronJamesAttributes[2] + "\n" + "Free Throw" + lebronJamesAttributes[3] + "\n" + "Offensive Rebound" + lebronJamesAttributes[4] + "\n" + "Defensive Rebound" + lebronJamesAttributes[5] + "\n" + "Assist" + lebronJamesAttributes[6] + "\n" + "Steal" + lebronJamesAttributes[7] + "\n");         
                                System.out.println("You have allocated a total of " + jamesTotalQuarter + " points.");}
                                break; // breaks from the loop on correct input

                            }

                       }

            }

暂无
暂无

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

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