我正在寫一個代碼,從標準輸入中讀取一個實數作爲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;
}
}
}
是不是70等於1x五十和1x二十?爲什麼tmp在被轉換爲int時會翻倍?爲什麼數量乘以100?我不認爲金額/ x,在x下工作1。 – Froziph
噢拍攝你讓我感謝點出 – javakook