public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
double budget, total, expenseTotal;
System.out.print("Enter your budget for the month: ");
budget = keyboard.nextDouble();
System.out.println("Type -99 to stop calculations\n");
byte start = 1;
double expense = 0;
while (expense != -99) {
System.out.print("Enter expense " + start + " :");
expense = keyboard.nextDouble();
start++;
if (expense == -99) {
System.out.println();
total = (budget - expense) - 99;
System.out.printf("Your current total budget is: $%,.2f \n" , total);
}
}
}
電流輸出:如何改變一個循環外部變量
輸入您的月預算:1000 類型-99停止計算
輸入費用1:100
輸入費用2:-99
您目前的總預算是:$ 1,000.00
所需的輸出:
您當前的預算總額爲:$ 900.00
問題:費用被宣佈while語句的外面,給我想通了數字0,由於費用= keyboard.nextDouble();在循環中,它會更新放置在循環外部的費用,但它沒有。那麼任何解決方案謝謝。
是什麼讓你相信它沒有更新?你是否在鍵盤輸入後立即嘗試'println'? –
因爲 合計=(預算 - 費用) - 99; –
你的問題是'nextDouble()'在使用後不會跳過一行。請參閱鏈接。 –