我有這樣的代碼:爲什麼我的代碼不能運行?
import java.util.Scanner;
public class PositiveNegative { public static void main(String[] args) {
int numbers, plus = 0, minus = 0;
int count = 0;
double total = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while(numbers != 0)
{
total += numbers;
if(numbers > 0)
plus++;
if(numbers < 0)
minus++;
}
System.out.println("The number of positives is: " +plus);
System.out.println("The number of negatives is: " +minus);
System.out.println("The number of total is: " +total);
}
}
的問題是,我嘗試運行它,並鍵入數字,但它什麼都不做。我需要它,這樣當你鍵入0時,它停止接收數字並開始處理代碼。我該怎麼辦?
你永遠不修改'numbers',所以一旦它進入循環,它永遠不會離開 – MadProgrammer
如果你輸入的數字不是0,那麼你被困在一個**無盡**循環中。只需在運行程序時檢查CPU使用情況......您還需要在循環中有'numbers = scan.nextInt()'。 – bmk