我是一名正在參加Java編程課程的學生。我的老師要求我們使用JOption
窗格進行練習。我認爲我擁有大部分的功能,但無法使其正常工作。我的程序不接受JOption
窗格中的選項,但它在CMD窗口中接受它們。在JOption
窗格被清除之後,選項被接受。我的代碼是:Java中的掃描器輸入問題
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Coffee10 {
private static int item;
private static double price;
private static int menuNum;
private static Scanner input;
public static void main(String[] args) {
calculateP();
if (menuNum > 0) //calculateP(); going for 3 instances of input
{
if (menuNum > 0) //calculateP();
{
if (menuNum > 0) {
calculateP();
}
}
}
}
public static double calculateP() {
Scanner input = new Scanner(System.in);
JOptionPane.showInputDialog(null, "Jivin' Java Coffee Shop" + "\nCoffee" + "\n(1)American 1.99" + "\n(2)Expresso 2.50" + "\n(3)Latte 2.15" + "\nMake selection. Press 0 to quit");
JOptionPane.showInputDialog(null, "Jivin' Java Coffee Shop" + "\nCoffee" + "\n(1)American 1.99" + "\n(2)Expresso 2.50" + "\n(3)Latte 2.15" + "\nMake selection. Press 0 to quit");
JOptionPane.showInputDialog(null, "Jivin' Java Coffee Shop" + "\nCoffee" + "\n(1)American 1.99" + "\n(2)Expresso 2.50" + "\n(3)Latte 2.15" + "\nMake selection. Press 0 to quit");
JOptionPane.showMessageDialog(null, "Your total order comes to $" + price + ".");
menuNum = input.nextInt();
if (menuNum == 1) {
item += 1;
price += 1.99;
return 1.99;
} else if (menuNum == 2) {
item += 1;
price += 2.50;
return 2.50;
} else if (menuNum == 3) {
item += 1;
price += 2.15;
return 2.15;
} else {
return 0;
}
}
}
請格式化你的代碼。這是不可讀的。 – 2014-09-22 14:20:58
註釋掉的代碼是怎麼回事('calculateP()')? – Holloway 2014-09-22 14:24:58
它正在啓動3次JOption窗格,只希望它出現1次。試錯。 – user3855016 2014-09-22 14:37:47