如果声明无限循环

时间:2014-12-14 06:38:44

标签: java loops infinite-loop

首先:我在以下代码中继续遇到无限循环。构建代码是为了输入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.");}

                }


                }
}

1 个答案:

答案 0 :(得分:0)

如果以下代码解决了您的问题,请告诉我。没有测试过,但它现在应该工作正常。获取逻辑并自行修复错误。

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

                            }

                       }

            }