2014-12-14 4 views
0

첫 번째 : 다음 코드에서 무한 루프가 계속 실행됩니다. 이 코드는 1에서 10 사이의 8 개의 숫자를 입력 할 수 있도록 작성되었습니다.이 숫자가 1에서 10 사이임을 증명합니다. 그런 다음 두 번째 증명은이 8 개의 숫자가 함께 더해진다는 것입니다. 성명서. 숫자의 합계가 60이면 제대로 작동하고 인쇄물을 제공하지만 if 문에서 중지되면 "잘못된 총 숫자를 입력했습니다"라는 내용의 인쇄물과 계속 증가하는 총 수를 무한히 인쇄합니다 포인트의. 나는 괄호 같은 무엇인가 쉽게 놓치고 있냐?무한 루프 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.");} 

       } 


       } 
} 
+0

for 루프는'lebronJamesAttributes' 배열의 요소를 기반으로하여'jamesTotalQuarter'를 추가합니다. 60을 뛰어 넘으면 무한 루프가 될 수 있습니다. 예를 들어'jamesTotalQuarter = 55'에 있고'lebronJamesAttributes'의 다음 항목은'10'입니다. 당신은'65'을 얻습니다. 그리고 숫자를 추가하기 때문에 항상 '60'이되지 않습니다. –

+0

출력을 얻으려면 재귀 적 기능을 만들어야합니다. – Panther

+0

Spamcer Wieczorek는 jamesTotalQuarter>와 같은 일종의 불평등을 표현해야합니까? jamesTotalQuarter <60? – user4343023

답변

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 

          } 

         } 

      } 
+0

끊기를 추가해도 작동하지만 계속하면 다른 무한 루프가 발생합니다. – user4343023

+0

@ user4343023 "총계가 60이 아니고 전체 프로세스를 다시 시작하면 원래 입력 값으로 되돌릴 수 있습니까?" 따라서 총계가 60이 아닌 경우 처음부터 루프를 시작하고 사용자에게 입력을 요청합니다. 그것이 당신이 원하는 것이 아닌 경우, 총액이 60이 아닐 때 프로그램이 기대하는 바를 설명해 주시겠습니까? –

+0

Ashwin Surana 총계가 60이 아닐 때 프로그램은 계속 "당신은 틀린 총계를 입력했습니다 ....."와 총계를 무한대로 인쇄합니다. 총! = 60 일 때 "속성 점수가 잘못 입력되었습니다 .8 특성 사이에 총 60 개의 속성 점수를 입력하십시오"라는 메시지가 인쇄됩니다. "총"+ jamesTotalQuarter + "포인트를 할당했습니다." 그런 다음 다시 값을 입력하는 사용자의 시작 부분으로 되돌아갑니다. – user4343023