编写货币计算器的Java首先停止在JOptionPane并继续运行

时间:2017-01-29 15:13:28

标签: java joptionpane

我是一名学习Java编程的新生。本周我们有一个使用两个类来编程货币计算器的任务。我在运行程序时遇到了一些问题,即保持运行。我们必须使用JOptionPane来请求输入和显示输出,我不完全确定我应该如何保持它们"弹出"。我希望我能得到一些关于如何保持程序运行的指示。

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

public class runner {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

        do
            {
                JOptionPane.showInputDialog(null, "Type in the number of kroner you want to check: ");
                double kroner = input.nextInt();
                Object[] options = {"Euro", "Yen"};
                int choice = JOptionPane.showOptionDialog(null, "you wrote: " + kroner + "do you want to convert this to euro or yen?", "Choose an alternative", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);

            Functions operationObject = new Functions();

            if (choice == 0)
            {
                operationObject.kronerTilEuro(kroner);
            }
            else if (choice == 1)
            {
                operationObject.kronerTilYen(kroner);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "You need to pick one of the alternatives!");
            }
        }
        while (!input.hasNextInt());
        {
            JOptionPane.showMessageDialog(null, "I am not happy");
            input.next();
        }
    }
}

我试图创建一个循环,以便第一个面板会再次出现,如果用户提供的输入不是整数,但没有任何运气。如果有遗漏的话,请告诉我。

2 个答案:

答案 0 :(得分:0)

问题出在这里,你正在使用来自用户的showInputDialog读书,之后你的程序中没有任何警告input.nextInt(),它会等到你输入一个数字:< / p>

JOptionPane.showInputDialog(null, "Type in the number of kroner you want to check: ");
double kroner = input.nextInt();

首先用input.nextInt()替换input.nextDouble(),您正在阅读double而不是int

我会告诉你两种方式:

1 - 将showInputDialog替换为showMessageDialog

JOptionPane.showMessageDialog(null, "Type in the number of kroner you want to check: ");
double kroner = in.nextDouble();

2 - 尝试使用showInputDialog阅读并将其String解析为Double

double kroner = Double.parseDouble(JOptionPane.showInputDialog(null, "Type in the number of kroner you want to check: "));

答案 1 :(得分:0)

我认为这就是你想要的

public class Runner {

    public static void main(String[] args) {
        // amount
        Double amount = null;
        while (amount == null) {
            String amountStr = JOptionPane.showInputDialog(null, "Type in the numeric amount kroner oyu want to check");
            if(amountStr==null)
                JOptionPane.showMessageDialog(null, "Pleae enter a numeric amount", "Error", JOptionPane.ERROR_MESSAGE);
            else
                try {
                    amount = Double.valueOf(amountStr);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "only 0-9 and . are allowed", "Error", JOptionPane.ERROR_MESSAGE);
                }
        }
        // option
        String[] options = new String[] { "-> EUR", "-> YEN" };
        int selection = JOptionPane.showOptionDialog(null, "Please select the target currency", "Title",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
        String option = options[selection];
        // result
        JOptionPane.showMessageDialog(null, doSomethingFancy(amount, option), "Result",
                JOptionPane.INFORMATION_MESSAGE);

    }

    private static String doSomethingFancy(Double amount, String option) {
        // here you want to do the calculations
        return amount + " kroener " + option;
    }

}