2015-02-05 78 views
0

我在編程課上領先一點,並且抓住了我無法想象的障礙,並且我嘗試了一切。基本上我們必須製造一臺只接受美元鈔票的自動售貨機,然後給用戶他們的改變。我已經評論了我無法弄清楚如何去做的部分。如果有人能指引我,那將是非常棒的方向!卡在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); 



} 
} 

回答

0

神諭API頁面通常是爲這些問題真正的好來源。這裏有一個應該在這裏幫助:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

你不能解析長度超過1個字符的字符串到一個字符。

爲了幫助您指出正確的方向,請嘗試調試'machineString'變量發生了什麼。將System.out.println添加到控制檯,然後可以看到如何比較用戶選擇的內容與if語句。

+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!"); 

} 
+0

我仍然就行 炭選擇= Character.parseChar(machineString)得到一個錯誤; //確定選擇 –

+0

檢查編輯 – Rush2sk8

0

首先,你聲明一個字符串作爲字符。

變化:

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); 
    }