坚持JOptionPane为类项目

时间:2015-02-05 00:14:18

标签: java joptionpane

我要比编程课程领先一点,我已经陷入了一个我无法弄清楚的障碍,我已经尝试了一切。基本上我们必须制作一台只接受美元钞票然后向用户提供更改的自动售货机。我评论过我无法弄清楚该怎么做的部分。如果有人能指出我正确的方向,那就太棒了!

import javax.swing.JOptionPane;
public class ChangeMaker
{
    public static void main(String[] args)
{

char a = "Water";
char b = "Juice";
char c = "Candy Bar";
char d = "Enery Bar";


    String machineString = JOptionPane.showInputDialog("Jonathan's Vending Machine\n" + "Choose a selection below\n" + "(1) Water ~ $0.35\n" + "(2) Juice ~ $0.50\n" + "(3) Candy Bar ~ $0.75\n" + "(4) Energy Bar ~ $0.95\n");

    int quarters;
    int nickels;
    int dimes;
    int pennies;
    char selection = Character.parseChar(machineString); //Determine selection
    int amount = 0;

    if (selection == 1) 
    {
        amount = 25;
    } else if (selection == 2) 
    {
        amount = 50;
    } else if (selection == 3)
    {
        amount = 75;
    } else if (selection == 4)
    {
        amount = 95;
    }


    quarters = amount / 25;
    amount = amount % 25;
    dimes = amount / 10;
    amount = amount % 10;
    nickels = amount / 5;
    amount = amount % 5;
    pennies = amount;

    JOptionPane.showMessageDialog(null,  "You selected " + selection + " your change is:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels and\n" + pennies + " pennies\n" + "Enjoy yout treat!");
    System.exit(0);



}
}

工作代码

import javax.swing.JOptionPane;
public class ChangeMaker
{
    public static void main(String[] args)
{


    int amount = 0;


    String machineString = JOptionPane.showInputDialog("Jonathan's Vending Machine\n" + "Choose a selection below\n" + "(A) Water ~ $0.35\n" + "(B) Juice ~ $0.50\n" + "(C) Candy Bar ~ $0.75\n" + "(D) Energy Bar ~ $0.95\n");


    String selection = "null";
    int quarters;
    int nickels;
    int dimes;
    int pennies;


    if (machineString.equals("a")) 
    {
        amount = 75;
        selection = "Water";
    } else if (machineString.equals("b")) 
    {
        amount = 50;
        selection = "Juice";
    } else if (machineString.equals("c"))
    {
        amount = 25;
        selection = "Candy Bar";
    } else if (machineString.equals("d"))
    {
        amount = 5;
        selection = "Energy Bar";
    }


    quarters = amount / 25;
    amount = amount % 25;
    dimes = amount / 10;
    amount = amount % 10;
    nickels = amount / 5;
    amount = amount % 5;
    pennies = amount;

    JOptionPane.showMessageDialog(null,  "You selected " + selection + " your change is:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels and\n" + pennies + " pennies\n" + "Enjoy yout treat!");
    System.exit(0);



}
}

3 个答案:

答案 0 :(得分:0)

public static void main(String[] args) {

String a = "Water";
String b = "Juice";
String c = "Candy Bar";
String d = "Enery Bar";

int quarters;
int nickels;
int dimes;
int pennies;
int selection = Integer.parseInt(JOptionPane.showInputDialog("Jonathan's Vending Machine\n" + "Choose a selection below\n" + "(1) Water ~ $0.35\n" + "(2) Juice ~ $0.50\n" + "(3) Candy Bar ~ $0.75\n" + "(4) Energy Bar ~ $0.95\n"));
int amount = 0;

if (selection == 1) 
    amount = 25;
 else if (selection == 2) 
    amount = 50;
 else if (selection == 3) 
    amount = 75;
 else if (selection == 4) {
    amount = 95;

quarters = amount / 25;
amount = amount % 25;
dimes = amount / 10;
amount = amount % 10;
nickels = amount / 5;
amount = amount % 5;
pennies = amount;

JOptionPane.showMessageDialog(null, "You selected " + selection + " your change is:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels and\n" + pennies + " pennies\n" + "Enjoy yout treat!");

}

答案 1 :(得分:0)

oracle API页面通常是这些问题的良好来源。这是一个应该有帮助的地方:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

您无法将长度超过1个字符的字符串解析为字符。

为了帮助您指明正确的方向,请尝试调试“machineString”变量所发生的情况。将System.out.println添加到控制台,然后您可以看到如何比较用户选择的内容与if语句。

答案 2 :(得分:0)

首先,您将String声明为char。

变化:

char a = "Water";
char b = "Juice";
char c = "Candy Bar";
char d = "Enery Bar";

要:

String a = "Water";
String b = "Juice"
String c = "Candy Bar";
String d = "Enery Bar";

由于你的if语句检查决定是否使用整数,你应该使用整数变量而不是字符。然后使用Integer.parseInt(machineString)

以下是整个代码:

public static void main(String[] args) {

    String a = "Water";
    String b = "Juice";
    String c = "Candy Bar";
    String d = "Enery Bar";


    String machineString = JOptionPane.showInputDialog("Jonathan's Vending Machine\n" + "Choose a selection below\n" + "(1) Water ~ $0.35\n" + "(2) Juice ~ $0.50\n" + "(3) Candy Bar ~ $0.75\n" + "(4) Energy Bar ~ $0.95\n");

    int quarters;
    int nickels;
    int dimes;
    int pennies;
    int selection = Integer.parseInt(machineString);
    int amount = 0;

    if (selection == 1) {
        amount = 25;
    } else if (selection == 2) {
        amount = 50;
    } else if (selection == 3) {
        amount = 75;
    } else if (selection == 4) {
        amount = 95;
    }


    quarters = amount / 25;
    amount = amount % 25;
    dimes = amount / 10;
    amount = amount % 10;
    nickels = amount / 5;
    amount = amount % 5;
    pennies = amount;

    JOptionPane.showMessageDialog(null, "You selected " + selection + " your change is:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels and\n" + pennies + " pennies\n" + "Enjoy yout treat!");
    System.exit(0);
    }