첫 번째 : 다음 코드에서 무한 루프가 계속 실행됩니다. 이 코드는 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.");}
}
}
}
for 루프는'lebronJamesAttributes' 배열의 요소를 기반으로하여'jamesTotalQuarter'를 추가합니다. 60을 뛰어 넘으면 무한 루프가 될 수 있습니다. 예를 들어'jamesTotalQuarter = 55'에 있고'lebronJamesAttributes'의 다음 항목은'10'입니다. 당신은'65'을 얻습니다. 그리고 숫자를 추가하기 때문에 항상 '60'이되지 않습니다. –
출력을 얻으려면 재귀 적 기능을 만들어야합니다. – Panther
Spamcer Wieczorek는 jamesTotalQuarter>와 같은 일종의 불평등을 표현해야합니까? jamesTotalQuarter <60? – user4343023