2015-09-28 67 views
0

我正在寫一個代碼,從標準輸入中讀取一個實數作爲double,然後獲取該值並打印出最少數量的帳單並投入所需的金額。我不確定如何在必要時將代碼打印出來並將複數數量的數字打印出來。如果有人能聽到我會很感激!Java更改計算器輸出

import java.util.Scanner; 
import java.lang.Math; 
public class Changecalc { 
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter amount) "); 
    double amount = input.nextDouble(); 
    int amountInt = (int) (amount * 100); 

    double tmp; 
    if(amount >= 100) 
     { 
     tmp = (int) amount/100; 
     System.out.println (tmp + "$100 bills"); 
     amount = amount % 100; 
     } 
    if(amount >= 50) 
     { 
     tmp = (int) amount/50; 
     System.out.println (tmp + "$50 bills"); 
     amount = amount % 50; 
     } 
    if(amount >= 20) 
    { 
     tmp = (int) amount/50; 
     System.out.println (tmp + "$20 bills"); 
     amount = amount % 20; 
    } 
    if(amount >= 10) 
    { 
     tmp= (int) amount/50; 
     System.out.println (tmp + "$10 bills"); 
     amount = amount % 10; 
    } 
    if(amount >= 5) 
    { 
     tmp = (int) amount/5; 
     System.out.println (tmp + "$5 bills"); 
     amount = amount % 5; 
    } 
    if(amount >= 1) 
    { 
     tmp = (int) amount/1; 
     System.out.println (tmp + "$1 bills"); 
     amount = amount % 1; 
    } 
    if(amount >= .25) 
    { 
     tmp = (int) amount/.25; 
     System.out.println (tmp + "quater"); 
     amount = amount % .25; 
    } 
    if(amount >= .10) 
    { 
     tmp = (int) amount/.10; 
     System.out.println (tmp + "dime"); 
     amount = amount % .10; 
    } 
    if(amount >= .05) 
    { 
     tmp = (int) amount/.05; 
     System.out.println (tmp + "nickel"); 
     amount = amount % .05; 
    } 
    if(amount >= .01) 
    { 
     tmp = (int) amount/.01; 
     System.out.println (tmp + "penny"); 
     amount = amount % 01; 
    } 
} 
} 
+0

是不是70等於1x五十和1x二十?爲什麼tmp在被轉換爲int時會翻倍?爲什麼數量乘以100?我不認爲金額/ x,在x下工作1。 – Froziph

+0

噢拍攝你讓我感謝點出 – javakook

回答

1

您的代碼大部分都是正確的。它只是在量> = 20,那麼你有

if(amount >= 20) 
{ 
    tmp = (int) amount/50; 
    System.out.println (tmp + "$20 bills"); 
    amount = amount % 20; 
} 

相反,它應該是

if(amount >= 20) 
{ 
    tmp = (int) amount/20; 
    System.out.println (tmp + "$20 bills"); 
    amount = amount % 20; 
} 

類似問題時量> = 10。您需要將其更正爲 。

除了這個,你需要有邏輯印刷紙幣(在多張鈔票的情況下),並具有類似的邏輯四(S),硬幣(S),鎳(S)&便士/便士


對於pluralising可以使用類似的東西以下

if(amount >= 100){ 
    tmp = (int) amount/100;   
    if(tmp > 1.0){ 
     System.out.println (tmp + "$100 bills"); 
    } else{ 
     System.out.println (tmp + "$100 bill"); 
    } 
    amount = amount % 100; 
} 
+0

這就是我需要一些幫助的部分,我會使用什麼類型的打印帳單? – javakook

+0

@patm更新了我的答案。您可以試試 –

+0

@patm如果這解決了您的查詢,那麼您可以接受答案 –

0

您正在使用錯誤的迪遮陽板。使用合適的一個,如:

if(amount >= 20) 
{ 
    tmp = (int) amount/20; 
    System.out.println (tmp + "$20 bills"); 
    amount = amount % 20; 
} 
if(amount >= 10) 
{ 
    tmp= (int) amount/10; 
    System.out.println (tmp + "$10 bills"); 
    amount = amount % 10; 
} 

,而不是這樣的:

tmp = (int) amount/50; 

這裏是您可以在您的格式做打印結果:

double tmp; 
      String result = "Amount: "; 

      if(amount >= 100) 
      { 
       tmp = (int) amount/100; 
       result = result + (int)tmp + "$100 bills "; 
       amount = amount % 100; 
      } 
      if(amount >= 50) 
      { 
       tmp = (int) amount/50; 
       result = result + (int)tmp + "$50 bills "; 
       amount = amount % 50; 
      } 
      if(amount >= 20) 
      { 
       tmp = (int) amount/20; 
       result = result + (int)tmp + "$20 bills "; 
       amount = amount % 20; 
      } 
      if(amount >= 10) 
      { 
       tmp= (int) amount/10; 
       result = result + (int)tmp + "$10 bills "; 
       amount = amount % 10; 
      } 
      if(amount >= 5) 
      { 
       tmp = (int) amount/5; 
       result = result + (int)tmp + "$5 bills "; 
       amount = amount % 5; 
      } 
      if(amount >= 1) 
      { 
       tmp = (int) amount/1; 
       result = result + (int)tmp + "$1 bills "; 
       amount = amount % 1; 
      } 
      if(amount >= .25) 
      { 
       tmp = (int) amount/.25; 
       result = result + (int)tmp + " qurters "; 
       amount = amount % .25; 
      } 
      if(amount >= .10) 
      { 
       tmp = (int) amount/.10; 
       result = result + (int)tmp + " dimes "; 
       amount = amount % .10; 
      } 
      if(amount >= .05) 
      { 
       tmp = (int) amount/.05; 
       result = result + (int)tmp + " nickels "; 
       amount = amount % .05; 
      } 
      if(amount >= .01) { 
       tmp = (int) amount/.01; 
       result = result + (int)tmp + " penny "; 
       amount = amount % 01; 
      } 
      System.out.println(result); 
     } 

這裏是運行示例:

輸入金額)124金額:1 $ 100賬單1 $ 20賬單4 $ 1賬單

您可以根據您的要求做一些細微的修改。

+0

以上的代碼哦,這是一個意外感謝指出。我的問題是,我如何爲整個事件建立一個印刷聲明,例如$ 70 = 1五十,一二十 – javakook

1

您應該更改代碼邏輯以打印出具有複數特定數量的金額。您可以嘗試下面的代碼,它可能會幫助你

import java.util.Scanner; 
import java.lang.Math; 
class Changecalc { 
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter amount) "); 
    double amount = input.nextDouble(); 
    int amountInt = (int) (amount * 100); 

    double tmp; 
    if(amount >= 100) 
     { 
     tmp = (int) amount/100; 
     System.out.println (tmp + "$100 bills"); 
     amount = amount % 100; 
     } 
    if(amount >= 50) 
     { 
     tmp = (int) amount/50; 
     System.out.println (tmp + "$50 bills"); 
     amount = amount % 50; 
     } 
    if(amount >= 20) 
    { 
     tmp = (int) amount/20; 
     System.out.println (tmp + "$20 bills"); 
     amount = amount % 20; 
    } 
    if(amount >= 10) 
    { 
     tmp= (int) amount/10; 
     System.out.println (tmp + "$10 bills"); 
     amount = amount % 10; 
    } 
    if(amount >= 5) 
    { 
     tmp = (int) amount/5; 
     System.out.println (tmp + "$5 bills"); 
     amount = amount % 5; 
    } 
    if(amount >= 1) 
    { 
     tmp = (int) amount/1; 
     System.out.println (tmp + "$1 bills"); 
     amount = amount % 1; 
    } 
    if(amount >= .25) 
    { 
     tmp = (int) amount/.25; 
     System.out.println (tmp + "quater"); 
     amount = amount % .25; 
    } 
    if(amount >= .10) 
    { 
     tmp = (int) amount/.10; 
     System.out.println (tmp + "dime"); 
     amount = amount % .10; 
    } 
    if(amount >= .05) 
    { 
     tmp = (int) amount/.05; 
     System.out.println (tmp + "nickel"); 
     amount = amount % .05; 
    } 
    if(amount >= .01) 
    { 
     tmp = (int) amount/.01; 
     System.out.println (tmp + "penny"); 
     amount = amount % .01; 
    } 
} 
}