Java,对货币计算器进行编程,首先停在 JOptionPane 并保持 运行
Java, programming a Currency Calculator, stops at first JOptionPane and keeps running
我是一名正在尝试学习 Java 编程的新生。本周我们有一项作业是使用两个 类 编写一个货币计算器。我在 运行 程序时遇到了一些问题,即让它继续运行。我们必须使用 JOptionPane 来请求输入和显示输出,我不完全确定我应该如何保留它们 "popping up"。我希望我能得到一些关于我应该如何保留程序的指示 运行。
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();
}
}
}
我曾尝试创建一个循环,以便在用户输入的不是整数但运气不佳时再次显示第一个面板。如果我遗漏了什么,请告诉我。
问题出在这里,您正在使用 showInputDialog
从用户那里读取某物,之后您的程序中有一个 input.nextInt()
而没有任何警告,它一直在等待您输入数字:
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: "));
我想这就是你想要的
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;
}
}
我是一名正在尝试学习 Java 编程的新生。本周我们有一项作业是使用两个 类 编写一个货币计算器。我在 运行 程序时遇到了一些问题,即让它继续运行。我们必须使用 JOptionPane 来请求输入和显示输出,我不完全确定我应该如何保留它们 "popping up"。我希望我能得到一些关于我应该如何保留程序的指示 运行。
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();
}
}
}
我曾尝试创建一个循环,以便在用户输入的不是整数但运气不佳时再次显示第一个面板。如果我遗漏了什么,请告诉我。
问题出在这里,您正在使用 showInputDialog
从用户那里读取某物,之后您的程序中有一个 input.nextInt()
而没有任何警告,它一直在等待您输入数字:
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: "));
我想这就是你想要的
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;
}
}