0
사용자가 음수 값을 입력 할 때 멈추는 프로그램을 작성하려고합니다. 그러나 사용자가 음수를 입력해도 루프는 계속 실행됩니다. 이유를 모르겠다. 도와주세요!Java Sentinel-controlled 루프가 작동하지 않습니다.
int input, smallest = 0, largest=0, total = 0, count = 1;
double avg;
System.out.print("Enter a number: ");
input = keyboard.nextInt();
while(input > 0)
{
System.out.print("Enter a number: ");
largest = keyboard.nextInt();
count++;
if (input > largest)
{
largest = input;
}
else
{
smallest = input;
}
total = total + largest + smallest;
}
avg = total/count;
System.out.println("Largest number is: " + largest);
System.out.println("Smallest number is: " + smallest);
System.out.printf("Average number is: %.2f \n", avg);
디버거는이 같은 상황에서 무슨 일이 일어나고 있는지 탐험을위한 훌륭한 도구입니다. –
OP가 '입력'에 할당 할 때 '최대'로 지정되는 오타처럼 보입니다. – markspace
한 번 코드를 확인해 봤어 ?????? –