如何在循环外更改变量

时间:2016-04-19 23:01:34

标签: java loops if-statement while-loop

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();在循环中,它会更新放在循环外部的费用,但事实并非如此。任何解决方案?谢谢。

4 个答案:

答案 0 :(得分:1)

尝试以下方法:

public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);
    double budget;


    System.out.print("Enter your budget for the month: "); 
    budget = keyboard.nextDouble();           
    System.out.println("Type -99 to stop calculations"); 
    System.out.println(); 
    byte start = 1; 

    double expense = 0;  

    while (expense != -99) {

        System.out.print("Enter expense " + start + " :"); 
        expense = keyboard.nextDouble(); 
        if (expense == -99) {
            System.out.println(); 
            System.out.printf("Your current total budget is: $%,.2f \n" , budget); 
        }else{
            budget = (budget - expense);
        }
        start++;             
    }
}

答案 1 :(得分:0)

相反

 expense = keyboard.nextDouble(); 

expense += keyboard.nextDouble(); 

相同
 expense = expense + keyboard.nextDouble(); 

应该解决所有问题。

答案 2 :(得分:0)

你提到费用是在while循环之外声明的,这是真的。但是你的 expenseTotal 在while循环之外声明了。我认为这是您打算存储累计费用总额的变量(代码中缺少这些费用)。对程序进行以下微小更改将产生所需的输出。

我添加了总计 expenseTotal 的初始化,添加了一行来累积 expenseTotal ,并更改了计算总计的行

public class Testprogram {

   public static void main(String[] args) {

        Scanner keyboard = new Scanner (System.in);
        double budget, total = 0, expenseTotal = 0;

        System.out.print("Enter your budget for the month: "); 
        budget = keyboard.nextDouble();           
        System.out.println("Type -99 to stop calculations"); 
        System.out.println(); 
        byte start = 1; 

        double expense = 0;  

        while (expense != -99) {

            System.out.print("Enter expense " + start + " :"); 
            expense = keyboard.nextDouble(); 

            expenseTotal = expenseTotal + expense;

            start++;             

        if (expense == -99) {
            System.out.println(); 

            //total = (budget - expense) - 99; 
            total = (budget - expenseTotal) - 99;
            System.out.printf("Your current total budget is: $%,.2f \n" , total); 
            }
        }
    }
}

答案 3 :(得分:0)

您的问题是费用变量。 我做了,如果他们输入0不是-99,只是因为我喜欢它:)。

while(true){
        System.out.print("Enter expense " + start + " :");
        double value = keyboard.nextDouble();
        if(value == 0){
            System.out.println();
            total = (budget - expense);
            System.out.printf("Your current total budget is: $%,.2f \n", total);
            break;
        }
        else{
            expense += value;
            start++;
        }
    }