Java 硬币计数器实验室
我是java的初学者。当我运行class&tester时,没有出现任何错误,但是我没有从测试仪获得我想要的输出。我找不到我做错了什么。请帮忙 这是我的密码:Java 硬币计数器实验室,java,swing,joptionpane,Java,Swing,Joptionpane,我是java的初学者。当我运行class&tester时,没有出现任何错误,但是我没有从测试仪获得我想要的输出。我找不到我做错了什么。请帮忙 这是我的密码: /** * 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 { // c
/**
* 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
这是我的测试仪:
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美分
int totalCents = pennies + nickels * PENNIES_PER_NICKEL....
到
系统退出(0)不应在main方法的末尾调用code>。一旦到达主方法的末尾,JVM将处理退出程序的问题。谢谢,但即使我去掉了它,我的输出仍然是sameYes,它不会改变你的输出。它只是值得注意的东西,因此我将其作为注释而不是回答发布。好的。谢谢你。你太棒了:D
this.totalCents = pennies + nickels....