首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoinCounter实验室

CoinCounter实验室
EN

Stack Overflow用户
提问于 2013-11-17 16:12:06
回答 1查看 177关注 0票数 0

我是java的初学者。当我运行这个类&测试器时,没有出现错误,但是我没有从测试人员那里得到我想要的输出。我找不到我做错了什么。请帮帮忙

这是我的密码:

代码语言:javascript
复制
/**
 * A CoinCounter has a specific number of cents.  It can provide the number of dollars and the
 * number of cents that it contains
 */
 public class CoinCounter
{
// constants
    public static final int PENNIES_PER_NICKEL = 5;
    public static final int PENNIES_PER_DIME = 10;
    public static final int PENNIES_PER_QUARTER = 25;
    public static final int PENNIES_PER_DOLLAR = 100;

// instance field (one - holds the total number of cents EX:  8,534)
    private int totalCents;

/**
 * Constructs a CoinCounter object with a specified number of pennies, nickels, dimes and quarters
 * @param pennies number of pennies
 * @param nickels number of nickels
 * @param dimes number of dimes
 * @param quarters numbr of quarters
 */
public CoinCounter(int pennies, int nickels, int dimes, int quarters)
/**
 * Computes the total value in pennies
 */
{
    int totalCents = pennies + nickels * PENNIES_PER_NICKEL + dimes * PENNIES_PER_DIME + quarters * PENNIES_PER_QUARTER;
}
// ACCESSOR methods as described (only two)
/**
 * Gets the number of dollars
 * @return number of dollars
 */
public int getDollars()
{
    int dollars = totalCents / PENNIES_PER_DOLLAR;
    return dollars;
}

/**
 * Gets the number of cents
 * @return number of cents
 */
public int getCents()
{
    int cents = totalCents % PENNIES_PER_DOLLAR;
    return cents;
}
//MUTATOR METHODS - NONE FOR THIS PROGRAM

这是我的测试器:

代码语言:javascript
复制
import javax.swing.JOptionPane;
/**
* A class to test the CoinCounter class
*/
public class CoinCounterTester
{
/**
* Tests methods of the CoinCounter class
* @param args not used
*/
public static void main(String[] args)
{
// Use JOptionPane to read in coins
    String input = JOptionPane.showInputDialog("Please enter the number of pennies: ");
    int pennies = Integer.parseInt(input);  

    input = JOptionPane.showInputDialog("Please enter the number of nickels: ");
    int nickels = Integer.parseInt(input);

    input = JOptionPane.showInputDialog("Please enter the number of dimes: ");
    int dimes = Integer.parseInt(input);

    input = JOptionPane.showInputDialog("Please enter the number of quarters: ");
    int quarters = Integer.parseInt(input);

    // Construct an object 
        CoinCounter myCoins = new CoinCounter(22, 8, 17, 5);
    //Call the TWO ACCESSOR METHODS-print dollars & cents
        System.out.println("The total dollars is: " + myCoins.getDollars());
        System.out.println("The total cents is:   " + myCoins.getCents());

        System.exit(0);
    //NO MUTATOR METHODS to test     
}
}

在运行测试器并输入每枚硬币的数量后,我的输出是

  • 美元总额为:0
  • 总分是:0

而不是

  • 总金额为:3
  • 一共是: 57美分。

我做错了什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-17 16:17:15

变化

代码语言:javascript
复制
int totalCents = pennies + nickels * PENNIES_PER_NICKEL....

代码语言:javascript
复制
 this.totalCents = pennies + nickels....
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20032986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档