问:数据类型选择
我的整数通话功能工作正常。但是,双重功能无法正常工作。需要帮忙。如果我输入数据选择3,它应该捕获double值并输出“您输入的数据类型正确,否则”您输入的数据类型不正确\ n请再试一次。“
需要为String值做同样的事情。如果我选择的数据类型1,则用户需要输入字符串值。除此之外,该程序必须输出“您输入不正确的数据类型\ n请再试一次”,如果下面的尝试字符串比输出正确的“数据类型您输入的是正确的“
主程序
import javax.swing.*;
public class Q1DataType {
public static int a, b, c, i;
public static double d, e;
public static void main(String[] args) {
for (c = 1; c > 0; c++) {
String a = JOptionPane
.showInputDialog("Data Type Validation Program\n\n\n 1). String\n 2). Integer\n 3). Double\n 4). Quit the program");
if ((b = Integer.parseInt(a)) == 2) {
callInteger mon = new callInteger();
}
if ((d = Double.parseDouble(a)) == 3) {
callDouble mon1 = new callDouble();
}
if ((i = Integer.parseInt(a)) == 4) {
System.exit(0);
}
}
}
}
整数类
import javax.swing.*;
public class callInteger {
public static int u;
public callInteger() {
try {
u = Integer.parseInt(JOptionPane
.showInputDialog("Please input Integer Data Type"));
JOptionPane.showMessageDialog(null,
"The Data Type You Entered is Correct");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"You Entered invalid Data Type\n Please Try Again");
}
}
}
双级
import javax.swing.*;
public class callDouble {
public static double t;
public callDouble() {
try {
t = Float.parseFloat(JOptionPane
.showInputDialog("Please Enter Double Data Type"));
JOptionPane.showMessageDialog(null,
"The Data Type You Entered is Correct");
} catch (Exception r) {
JOptionPane.showMessageDialog(null,
"You Entered incorrect Data Type\n Please try again");
}
}
}
为什么使用Float.parseFloat而不是Double.parseDouble? – Ted
我尝试了双倍,但不工作...任何想法请。相同的字符串不知道如何声明和输入。 – user3504969
任何人都可以尝试和帮助。 – user3504969