我想寫一個JAVA代碼,它保持接受整數,直到達到一個特定的數字,所以在這之後,用戶必須輸入0爲剩餘的輸入,以保持總和的輸入< =條件JAVA幫助獲取輸入總和
例如:如果我有5杯咖啡和5個可用的鏡頭,用戶爲第一杯咖啡輸入3個鏡頭,然後爲第二個杯子輸入2個鏡頭。所以現在3 + 2 = 5張是可用的咖啡照片的數量,所以對於接下來的3杯咖啡,用戶應該輸入0來繼續,否則它保持循環。
這是我的代碼看起來像:
int add = 0;
int[] numberOfCoffeeShots = new int[coffeeCupsAvailable]; //input number of shots for every coffee cup
int i; //declares i
for (i = 0; i < coffeCupsWanted; i++) { //iterate over a range of values.
System.out.print("How many coffee shots in cup " + (i + 1) + "? ");
numberOfCoffeeShots[i] = keyboard.nextInt();
add += (numberOfCoffeeShots[i]); //adding the number of shots in each cup
while (numberOfCoffeeShots[i] < 0) {
System.out.println("Does not compute. Try again.");
System.out.print("How many coffee shots in cup " + (i + 1) + "? ");
numberOfCoffeeShots[i] = keyboard.nextInt();
}
while (numberOfCoffeeShots[i] > coffeeShotsAvailable) {
System.out.println("There are only " + coffeeShotsAvailable + " coffee shots left. Try again.");
System.out.print("How many coffee shots in cup " + (i + 1) + "? ");
numberOfCoffeeShots[i] = keyboard.nextInt();
}
我仍然需要投入的總和,而循環> coffeeShotsAvailable
任何幫助,請這個想法?謝謝
[JAVA輸入的總和大於特定條件未接受]的可能重複(http://stackoverflow.com/questions/36479536/java-sum-of-inputs-greater-than-a-specific-condition-unaccepted ) –
我知道我想刪除舊的1抱歉 –