有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

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

    不能将长度超过1个字符的字符串解析为一个字符

    为了帮助您指出正确的方向,请尝试调试“machineString”变量的情况。添加一个系统。出来println到控制台,然后您可以看到如何比较用户选择的内容和if语句

  2. # 2 楼答案

    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!");
    
    }
    
  3. # 3 楼答案

    首先,将字符串声明为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语句中的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 ~ $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);
        }