Java 基于JOptionPane输入的打印结果
我是一名学生,正在努力完成一个家庭作业实验。现在,我正在努力使用来自JOptionPane的输入来显示基于输入的结果。我的实验室有两个文件,CoinCounterTester.java和CoinCounter.java。两者都可以编译,但它应该打印美元总数和美分总数(教师说明)“输出应该在控制台窗口中,并且应该演示和转义序列的使用”。我不确定测试仪的任何部分是否正确,但我认为JOptionPane方法是正确的。我还认为我应该解析它们,将它们转换成整数形式,但是我不知道如何根据用户输入打印指定的美元数和剩余的美分数。有人能解释一下吗 谢谢 编辑: 好的,答案似乎是正确的,但我对使用构造函数调用感到困惑。你会为构造函数调用的参数设置什么Java 基于JOptionPane输入的打印结果,java,joptionpane,Java,Joptionpane,我是一名学生,正在努力完成一个家庭作业实验。现在,我正在努力使用来自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;
}
}
您正在寻找的是一个构造函数调用。您拥有所有的值。您只需要创建一个CoinCounter来为您计算它们。这样做的示例如下:
CoinCounter coinCounter = new CoinCounter(1, 2, 3, 4);
拥有CoinCounter后,您可以对其调用方法,如CoinCounter.getCents()
。您可以使用System.out.println()
打印内容。这应该是您需要完成的三件事情
编辑:关闭!仔细看看你是如何调用构造函数的,以及我是如何调用的。你所做的是这样的:
CoinCounter coinCounter = new CoinCounter(int 1, int 2, int 3, int 4);
将其与上面的示例进行比较
您只在定义构造函数时才将变量类型放在那里,而不是在调用它时。是的,这似乎有帮助,让我试试。不幸的是,我仍然有问题。感谢您的建议,它最终消除了一些错误,但我仍然遇到问题。是的,我有点期待。您需要注意f在代码中的什么地方你尝试这样做。它必须在某个地方,变量
四分之一
,一角
,五分镍币
,和便士
是可见的。变量大部分只在最近的封闭大括号内可见--{}
。当您从JOptionPanes中获取值时,您在main方法中声明这些变量,因此该行必须在main方法中的大括号内的某个位置,但在声明所有变量之后,因为在声明变量之前,您不能使用该变量,并且在设置该变量之前,您不想使用它。此外,当您发布c在一个问题中,寻找小“{}”按钮。这将把代码格式化为代码,使其更易于阅读。