2016-04-19 80 views
0
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();在循環中,它會更新放置在循環外部的費用,但它沒有。那麼任何解決方案謝謝。

+0

是什麼讓你相信它沒有更新?你是否在鍵盤輸入後立即嘗試'println'? –

+0

因爲 合計=(預算 - 費用) - 99; –

+0

你的問題是'nextDouble()'在使用後不會跳過一行。請參閱鏈接。 –

回答

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++;    
    } 
} 
0

相反

expense = keyboard.nextDouble(); 

expense += keyboard.nextDouble(); 

這是一樣的

expense = expense + keyboard.nextDouble(); 

應該解決一切。

+0

這樣做會導致循環失去通過鍵入-99結束的能力。 –

+0

對。然後添加一個變量來增加:** expBuffer = keyboard.nextDouble();費用+ = expBuffer; **然後** while(expBuffer!= -99)。**不要忘記創建變量。 – Cthulchu

+0

已經有'expenseTotal' :) –

0

你提到費用是while循環,這是真正的外部聲明。但是您的費用總計之外的while循環聲明。而且我認爲這是您想要存儲累計費用總額的變量(代碼中缺少這些變量)。在你的程序下面做出這些小的改變將會產生你想要的結果。

我加的初始化expenseTotal,添加一行accummulate 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); 
      } 
     } 
    } 
} 
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++; 
     } 
    } 
相關問題