Java 简单的货币值,不确定如何输入和输出货币
编写一个封装硬币概念的类,假设硬币具有以下属性:一个四分之一硬币的数量、一个一角硬币的数量、一个五分镍币的数量和一个一分硬币的数量。包括构造函数、评估器和变异器,以及字符串和相等的方法 不确定如何执行此部分:Java 简单的货币值,不确定如何输入和输出货币,java,currency,Java,Currency,编写一个封装硬币概念的类,假设硬币具有以下属性:一个四分之一硬币的数量、一个一角硬币的数量、一个五分镍币的数量和一个一分硬币的数量。包括构造函数、评估器和变异器,以及字符串和相等的方法 不确定如何执行此部分: 还可以对以下方法进行编码:一种方法返回以美元表示的货币总额,小数点后有两个有效数字,另一种方法返回以25美分、10美分、5美分和1美分表示的货币。编写一个接受用户输入的客户机类来测试类中的所有方法 public class CoinsApp { public static void m
- 还可以对以下方法进行编码:一种方法返回以美元表示的货币总额,小数点后有两个有效数字,另一种方法返回以25美分、10美分、5美分和1美分表示的货币。编写一个接受用户输入的客户机类来测试类中的所有方法
public class CoinsApp { public static void main(String[] args) { Coins c = new Coins(); Scanner scan = new Scanner(System.in); System.out.print("Enter the number of Quarters: "); int q = scan.nextInt(); System.out.print("Enter the number of Dimes: "); int d = scan.nextInt(); System.out.print("Enter the number of nickels: "); int n = scan.nextInt(); System.out.print("Enter the number of pennies: "); int p = scan.nextInt(); Coins c1 = new Coins(q,d,n,p); System.out.println(c1); } }
private int quarters;
private int dimes;
private int nickles;
private int pennies;
public Coins() {
quarters = 0;
dimes = 0;
nickles = 0;
pennies = 0;
}
public Coins(int quarters, int dimes, int nickles, int pennies) {
this.quarters = quarters;
this.dimes = dimes;
this.nickles = nickles;
this.pennies = pennies;
}
/**
*
* @return the value of nickles
*/
public int getNickles() {
return nickles;
}
/**
*
* @param nickles
*/
public void setNickles(int nickles) {
this.nickles = nickles;
}
public int getPennies() {
return pennies;
}
public void setPennies(int pennies) {
this.pennies = pennies;
}
/**
* Get the value of dimes
*
* @return the value of dimes
*/
public int getDimes() {
return dimes;
}
/**
* Set the value of dimes
*
* @param dimes new value of dimes
*/
public void setDimes(int dimes) {
this.dimes = dimes;
}
/**
* Get the value of quarters
*
* @return the value of quarters
*/
public int getQuarters() {
return quarters;
}
/**
* Set the value of quarters
*
* @param quarters new value of quarters
*/
public void setQuarters(int quarters) {
this.quarters = quarters;
}
@Override
public String toString() {
return "Coins{" + "quarters=" + quarters + ", dimes=" + dimes + ", nickles=" + nickles + ", pennies=" + pennies + '}';
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Coins other = (Coins) obj;
if (this.quarters != other.quarters) {
return false;
}
if (this.dimes != other.dimes) {
return false;
}
if (this.nickles != other.nickles) {
return false;
}
if (this.pennies != other.pennies) {
return false;
}
return true;
}
}有很多方法可以解决这个问题。如果你想看起来聪明,你可以使用多态性,但是一个简单的解决方案要好得多 基本上,您有一个类(主类除外),我将其称为
wallet
:
public class Wallet{
private int dimes=0;
//...etc for all denominations
//We just print the total value, essentially counting coins here
//Not familiar with american coinage; in this example a dime=5c and a
//nickel=10c
public void printDollars(){
float total=0.0;
total+=this.dime*5;
total+=this.nickel*10;
//...etc
//Divide by 100 to get value in dollars instead of cents
total/=100;
//Now to format the output. Java's System.out.format works a lot
//like C's printf.
System.out.format("%.2f$", total);
}
}
更多信息,请查看
对于返回一角硬币、五分镍币等数字的另一部分,您只需使用getter即可。您得到的错误是什么?或者,如果你对某个特定的概念不清楚,也许把它放在上面会帮助我们引导你走向正确的方向:)我关心的是如何使用
float
来存储一些美元。在本例中它是有效的,但建议某人这样做是一件危险的事情——例如,下一个练习可能涉及将多个钱包
对象中的钱加在一起;如果金额是float
s,这将无法很好地工作。我的感觉是,堆栈溢出的答案应该尽量使这些细节正确。@DavidWallace您完全正确。我觉得浮点精度对于这个练习来说已经足够好了,但在一个会计应用程序中,我也不会使用它。另一种方法是通过执行int cents=total%100
(不考虑其中的任何off)获得美分数,对于整数合计,使用total-=cents获得美元数,然后分别打印它们。想想看,这是一个更好更简单的解决方案。