2016-09-23 127 views
-2

我應該寫一個程序,返回宿舍,鎳幣,硬幣和便士的變化。Edhesive AP計算機科學U1 Ex 2

但是,我只能得到變化和宿舍。

沒有其他改變的作品。

import java.math.*; 
import java.text.*; 
import java.util.*; 

public class Main { 
public static void main(String[] args) { 
Scanner scanner = new Scanner(System.in); 
double amountPaid, cost, changeOwed ; 
double pennies, nickels, dimes, quarters, change; 

System.out.println("Please Enter the Amount Paid"); 
amountPaid = scanner.nextDouble(); 

System.out.println("Please Enter the Total Cost of the Item:"); 
cost = scanner.nextDouble(); 

double roundOffPaid = amountPaid * 100; 
double roundOffCost = cost *100; 
double change2 = roundOffPaid - roundOffCost; 
change = change2/100; 
System.out.println(change); 


quarters = Math.floor(change/0.25); 
dimes = Math.floor(change % .25)/.10; 
nickels = Math.floor(change % .10)/.5; 
pennies = Math.floor(change % .5)/.1; 
System.out.println("Quarters: " +quarters); 
System.out.println("Dimes: "+ dimes); 
System.out.println("Nickels" + nickels); 
System.out.println("Pennies "+ pennies); 
    } 
} 
+2

將輸入轉換爲相應的「int」(乘以100然後轉換)。然後使用整數執行所有必需的操作。 – PEF

回答

0

代碼中沒有任何錯誤。當你通過將「變化」除以0.25,0.1和0.5得到餘數時,你總會得到0作爲結果。