JOptionPane.showInputDialog只出现一次

时间:2014-10-02 07:50:35

标签: java swing joptionpane

import java.util.Scanner;
import javax.swing.JOptionPane;

public class moneyRate {

    public static void main(String[] args) {
        //Get Inputs
        Scanner input = new Scanner(System.in);
        JOptionPane.showInputDialog("How many old pounds? ");
        double oldPounds = input.nextDouble();
        JOptionPane.showInputDialog("How many old shillings? ");
        double oldShillings = input.nextDouble();
        JOptionPane.showInputDialog("How many old pennies? ");
        double oldPennies = input.nextDouble();

        input.close();

        //New Pounds calc
        double newPounds = ((oldPounds*160.80) + (oldShillings*8.04) + (oldPennies*0.67));

        System.out.print("Your old pounds shillings and pennies are equal to £4"
                + "" + newPounds + ".");
    }
}

在一个编程课程中,我们被要求制作一个程序,告诉用户他们的旧磅先令和便士今天的价值是多少。我使用控制台作为程序的输入和输出完全正常工作,但现在当我尝试使用JOptionPane时,为用户提供小弹出框,它不会工作。当我运行任务时,只有第一个弹出窗口显示,程序刚刚结束,没有任何形式的错误消息。我假设这是一个简单的语法错误,但我无法发现它。

如果有人发现错误,请帮助我,谢谢:)

3 个答案:

答案 0 :(得分:1)

您使用JOptionPaneScanner的方式会导致问题。

JOptionPane.showInputDialog("How many old pounds? "); // display this
double oldPounds = input.nextDouble(); // then it wait for scanner input

现在你的程序将保留在那里以期待来自控制台的输入。您需要更改代码,如下所示

 double oldPounds = Double.parseDouble(JOptionPane.showInputDialog("How many old pounds? "));
 double oldShillings = Double.parseDouble(JOptionPane.showInputDialog("How many old shillings? "));
 double oldPennies = Double.parseDouble(JOptionPane.showInputDialog("How many old pennies? "));

 double newPounds = ((oldPounds*160.80) + (oldShillings*8.04) + (oldPennies*0.67));

 System.out.print("Your old pounds shillings and pennies are equal to £4"
            + "" + newPounds + ".");

答案 1 :(得分:1)

你在那做什么?显示对话框后,您将从命令行中读取。您应该从inputDialog中获取值。

答案 2 :(得分:0)

Ruchira答案已经完成..请注意      JOptionPane.showInputDialog() 返回一个字符串。

这就是你需要在代码中加入Double.parseDouble转换的原因。