我在編程課上領先一點,並且抓住了我無法想象的障礙,並且我嘗試了一切。基本上我們必須製造一臺只接受美元鈔票的自動售貨機,然後給用戶他們的改變。我已經評論了我無法弄清楚如何去做的部分。如果有人能指引我,那將是非常棒的方向!卡在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);
}
}
我想通了。我調整了代碼,讓一切正常工作。我編輯了與工作代碼的原始文章,以防您感興趣 謝謝! –