我在编程课上领先一点,并且抓住了我无法想象的障碍,并且我尝试了一切。基本上我们必须制造一台只接受美元钞票的自动售货机,然后给用户他们的改变。我已经评论了我无法弄清楚如何去做的部分。如果有人能指引我,那将是非常棒的方向!卡在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);
}
}
我想通了。我调整了代码,让一切正常工作。我编辑了与工作代码的原始文章,以防您感兴趣 谢谢! –