简体   繁体   English

根据JOptionPane的输入打印结果

[英]Printing Result based on Input from JOptionPane

I am student, trying to finish a homework lab. 我是学生,正在努力完成一个家庭作业。 Right now I am struggling to use the input from JOptionPane to display the results based on the input. 现在我正在努力使用JOptionPane的输入来显示基于输入的结果。 There are two files to my lab, the CoinCounterTester.java and the CoinCounter.java. 我的实验室有两个文件,CoinCounterTester.java和CoinCounter.java。 Both compile but it should print the total numbers of dollars and the total number of cents (teachers instructions) "Output should be in the console window, and should demonstrate the use of and escape sequences". 两者都编译但它应该打印美元总数和总分数(教师说明)“输出应该在控制台窗口中,并且应该演示使用和转义序列”。 I'm not sure if any part of the tester is correct but I think the JOptionPane methods are correct. 我不确定测试仪的任何部分是否正确但我认为JOptionPane方法是正确的。 I also think I'm supposed to parse them to get them into integer form , however I do not know how to print the designated number of dollars and number of cents left over based on user input. 我也认为我应该解析它们以使它们变成整数形式,但是我不知道如何根据用户输入打印指定数量的美元和剩余的分数。 Can anyone explain? 谁能解释一下?

Thanks 谢谢

Edit: Ok the answer seems like its correct but I'm confused about using the constructor call. 编辑:好的答案似乎是正确的但我对使用构造函数调用感到困惑。 What would you put for the parameters for the constructor call, I put 我把你为构造函数调用的参数放什么

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

but got seven errors 但有七个错误

Edit 2: 编辑2:

I have now not included the variable type as well wisely suggested and input 我现在没有包含变量类型以及明智的建议和输入

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

but I still get 4 errors (error cannot find symbol) :(. Can anyone suggest a correction please? 但我仍然得到4个错误(错误找不到符号):(。有人可以建议更正吗?

Edit 3: Added the println statements and moved the constructor call to the bottom but whenever I run the program I cannot get the file to print the number of dollars and the number of cents?! 编辑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;
    }


 }

What you're looking for is a constructor call. 您正在寻找的是构造函数调用。 You have all the values. 你拥有所有的价值观。 You just need to create a CoinCounter to count them for you. 您只需要创建一个CoinCounter来为您计算它们。 An example of doing that would look like: 这样做的一个例子如下:

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

After you have your CoinCounter, you can call methods on it, like coinCounter.getCents() . 拥有CoinCounter后,您可以在其上调用方法,例如coinCounter.getCents() You print things out using System.out.println(<whatever you want to print>) . 使用System.out.println(<whatever you want to print>)打印出来System.out.println(<whatever you want to print>) Those should be the three things you need to finish up. 那些应该是你需要完成的三件事。

Edit: Close! 编辑:关闭! Look carefully at how you called the constructor and how I did it. 仔细看看你如何调用构造函数以及我是如何调用它的。 What you did is like this: 你做的是这样的:

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

Compare it to my example above. 将它与我上面的例子进行比较。

You only put the variable type there when you define the constructor, not when you call it. 您只在定义构造函数时将变量类型放在那里,而不是在调用它时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM