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()'在使用后不会跳过一行。请参阅链接。 –