卡在 class 项目的 JOptionPane 上

Stuck on JOptionPane for class project

我的编程有点超前 class,但我遇到了一个我无法弄清楚的障碍,我已经尝试了所有方法。基本上我们必须制作一台只接受美元钞票然后给用户找零的自动售货机。我已经评论了我不知道该怎么做的部分。如果有人能为我指出正确的方向,那就太棒了!

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 ~ [=11=].35\n" + "(2) Juice ~ [=11=].50\n" + "(3) Candy Bar ~ [=11=].75\n" + "(4) Energy Bar ~ [=11=].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 ~ [=12=].35\n" + "(B) Juice ~ [=12=].50\n" + "(C) Candy Bar ~ [=12=].75\n" + "(D) Energy Bar ~ [=12=].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);



}
}
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 ~ [=10=].35\n" + "(2) Juice ~ [=10=].50\n" + "(3) Candy Bar ~ [=10=].75\n" + "(4) Energy Bar ~ [=10=].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!");

}

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

无法将超过 1 个字符的字符串解析为一个字符。

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

首先,您将字符串声明为字符。

变化:

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 语句 where check the decision 使用的是整数,因此您应该使用整数变量而不是字符。然后使用 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 ~ [=12=].35\n" + "(2) Juice ~ [=12=].50\n" + "(3) Candy Bar ~ [=12=].75\n" + "(4) Energy Bar ~ [=12=].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);
    }