gpt4 book ai didi

java - 根据 JOptionPane 的输入打印结果

转载 作者:行者123 更新时间:2023-12-02 08:08:41 25 4
gpt4 key购买 nike

我是学生,正在努力完成家庭作业。现在我正在努力使用 JOptionPane 的输入来显示基于输入的结果。我的实验室有两个文件,CoinCounterTester.java 和 CoinCounter.java。两者都编译,但它应该打印美元总数和美分总数(教师指示)“输出应该在控制台窗口中,并且应该演示使用和转义序列”。我不确定测试器的任何部分是否正确,但我认为 JOptionPane 方法是正确的。我还认为我应该解析它们以将它们转换为整数形式,但是我不知道如何根据用户输入打印指定的美元数和剩余的美分数。谁能解释一下吗?

谢谢

编辑:好吧,答案似乎是正确的,但我对使用构造函数调用感到困惑。你会为构造函数调用的参数设置什么,我设置了

CoinCounter coinCounter = new CoinCounter(int quarters, int dimes, int nickels, int pennies); 

但是有七个错误

编辑2:

我现在还没有包含明智建议和输入的变量类型

CoinCounter coinCounter = new CoinCounter(quarters, dimes, nickels, pennies);

但我仍然收到 4 个错误(错误找不到符号):(。有人可以建议更正吗?

编辑 3:添加了 println 语句并将构造函数调用移至底部,但每当我运行程序时,我无法让文件打印美元数和美分数?!

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)
{


String quarter = JOptionPane.showInputDialog("Enter the quantity of quarters");
int quarters = Integer.parseInt(quarter);

String dime = JOptionPane.showInputDialog("Enter the quantity of dimes");
int dimes = Integer.parseInt(dime);

String nickel = JOptionPane.showInputDialog("Enter the quantity of nickels");
int nickels = Integer.parseInt(nickel);

String penny = JOptionPane.showInputDialog("Enter the quantity of pennies");
int pennies = Integer.parseInt(penny);



CoinCounter coinCounter = new CoinCounter(quarters, dimes, nickels, pennies);

System.out.println(coinCounter.getDollars());
System.out.println(coinCounter.getCents());






System.exit(0);

}
}




/**
* 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
//*** These are class constants so they need public static
public static final int QUARTER_VALUE = 25;
public static final int DIME_VALUE = 10;
public static final int NICKEL_VALUE = 5;
public static final int PENNY_VALUE = 1;
public static final int PENNY_PER_DOLLAR_VALUE = 100;

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

/**
* Constructs a CoinCounter object with a specified number of pennies,
* nickels, dimes and quarters
* @param quarterAmount the amount of quarters
* @param dimeAmount the amount of dimes
* @param nickelAmount the amount of nickels
* @param pennyAmount the amount of pennies
*/
public CoinCounter(int quarters, int dimes, int nickels, int pennies)
{
total = quarters * QUARTER_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + pennies;



}
// add remaining methods as described

/**
* getDollars returns the number of dollars in the CoinCounter
* @return the number of dollars
*/
public int getDollars()
{
int dollars = (int) total / PENNY_PER_DOLLAR_VALUE;
return dollars;
}
/**
* getCents returns the number the numbers of cents left over after the dollars are removed
* @return the number of cents left over
*/
public int getCents()
{
int cents = total % PENNY_PER_DOLLAR_VALUE;
return cents;
}


}

最佳答案

您正在寻找的是构造函数调用。你拥有所有的值(value)观。您只需要创建一个 CoinCounter 来为您计数。这样做的一个例子如下:

CoinCounter coinCounter = new CoinCounter(1, 2, 3, 4);

有了 CoinCounter 后,您可以调用它的方法,例如 coinCounter.getCents() 。您可以使用 System.out.println(<whatever you want to print>) 打印内容。这些应该是您需要完成的三件事。

编辑:关闭!仔细看看你是如何调用构造函数的以及我是如何做到的。你所做的是这样的:

CoinCounter coinCounter = new CoinCounter(int 1, int 2, int 3, int 4);

将其与我上面的示例进行比较。

只有在定义构造函数时才将变量类型放在那里,而不是在调用它时。

关于java - 根据 JOptionPane 的输入打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7785279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com