【问题标题】:Simple Currency values, not sure how to take input and output currency简单的货币值,不确定如何输入和输出货币
【发布时间】:2014-02-25 23:10:55
【问题描述】:

编写一个封装硬币概念的类,假设硬币具有以下属性:硬币数量、硬币数量、镍币数量和便士数量。包括构造函数、评估器和修改器,以及 toString 和 equals 方法。

不知道如何做这部分:

  • 还对以下方法进行编码:一种以美元表示法返回总金额,小数点后两位有效数字,另一种以四分之一、一角、镍和便士的形式返回货币。编写一个接受用户输入的客户端类来测试类中的所有方法。

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

}

【问题讨论】:

  • 您遇到的错误是什么?或者,如果您对某个特定概念感到模糊,也许将其提出将有助于我们引导您朝着正确的方向前进 :)

标签: java currency


【解决方案1】:

有很多方法可以解决这个问题。如果你想看起来更聪明,你可以用多态来做到这一点,但一个简单的解决方案会更好。

基本上你有一门课(除了你的主课),我称之为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);
}

}

查看System.out.format 了解更多信息。

对于返回硬币、镍币等的数量的另一部分,您只需要使用 getter。

【讨论】:

  • 我担心在这里使用float 来存储一些美元。它在这个例子中有效,但是建议某人去做是一件危险的事情——例如,下一个练习可能涉及将几个 Wallet 对象的钱加在一起;如果金额为floats,这将无法正常工作。我的感觉是 Stack Overflow 的答案应该尽量把这些细节弄好。
  • @DavidWallace 你是绝对正确的。我觉得浮点精度对于这个练习来说已经足够好了,但在会计应用程序中我也会远离它。另一种方法是通过执行int cents=total % 100(忽略其中的任何非整数)来获取美分的数量,并使用total-=cents 获取整数总数,然后分别打印它们。想想看,这是一个更好、更简单的解决方案。
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多