问题:
我的整数调用函数运行正常。但是双功能不起作用。需要帮忙。如果我输入数据选择3它应该捕获双值并输出“您输入的数据类型是正确的其他”您输入的数据类型不正确\ 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");
}
}
}
答案 0 :(得分:0)
它仍然是相同的功能,但在这里。它就像你的代码一样工作:
import javax.swing.JOptionPane;
public class Q1DataType {
public static void main(String[] args) {
while (true) {
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 (a.equals("1")) {
// String
}
if (Integer.parseInt(a) == 2) {
new callInteger();
}
if (Double.parseDouble(a) == 3) {
new callDouble();
}
if (Integer.parseInt(a) == 4) {
System.exit(0);
}
}
}
}
编辑 :啊,我想我现在明白你想做什么了。问题是,它总是将其自动加密为double值。你可以尝试类似的东西。
import javax.swing.JOptionPane;
public class callDouble {
public static double t;
public callDouble() {
try {
String s = JOptionPane.showInputDialog("Please Enter Double Data Type");
if (s.contains(".")) {
t = Double.parseDouble(s);
JOptionPane.showMessageDialog(null, "The Data Type You Entered is Correct");
} else {
JOptionPane.showMessageDialog(null, "You Entered incorrect Data Type\n Please try again");
}
} catch (Exception r) {
JOptionPane.showMessageDialog(null, "You Entered incorrect Data Type\n Please try again");
}
}
}