我尝试在乘以2个字符串方面搜索多个线程。但是,似乎我发现的大部分链接都不适用,或者我无法很好地解释它们。
是否有可能告诉我当前代码有什么问题?
public static void main(String[] args) {
String sample = value1();
String sample2 = value2();
//========== Test if users placed any characters within the boxes =========\\
try {
Integer.parseInt(sample);
} catch (NumberFormatException e) {
System.out.println("System detects that you are using characters");
return;
}
try {
Integer.parseInt(sample2);
} catch (NumberFormatException e) {
System.out.println("System detects that you are using characters");
return;
}
Integer.parseInt(sample);
Integer.parseInt(sample2);
System.out.println("The total multiplication that you have inserted is "+sample * sample2+ ".");
}
public static String value1() { //obtain first user input.
String sample = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE);
if (sample.isEmpty()) {
JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
sample = value1();
}
return sample;
}
public static String value2() { //obtain second user input.
String sample2 = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE);
if (sample2.isEmpty()) {
JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
sample2 = value2();
}
return sample2;
}
}
我的最终输出应乘以以下数字
System.out.println("The total multiplication that you have inserted is "+sample * sample2+ ".");
答案 0 :(得分:1)
您不能将字符串相乘。你可以将数字相乘,听起来就像你想要做的那样。实际上,您已经将字符串解析为整数 - 然后忽略结果。你只需要改变这个:
Integer.parseInt(sample);
Integer.parseInt(sample2);
System.out.println("The total multiplication that you have inserted is "+sample * sample2+ ".");
为:
int value1 = Integer.parseInt(sample);
int value2 = Integer.parseInt(sample2);
System.out.println("The total multiplication that you have inserted is "
+ (value1 * value2) + ".");
答案 1 :(得分:0)
你试图将两个字符串乘以两个数字
你应该这样使用:
System.out.println("The total multiplication that you have inserted is "+ Integer.parseInt(sample) * Integer.parseInt(sample2)+ ".");
答案 2 :(得分:0)
您必须将Integer.parseInt()的返回值赋给变量。作为参数传递的变量保持不变。
int a = Integer.parseInt(sample);
int b = Integer.parseInt(sample2);
System.out.println("The total multiplication that you have inserted is " + a * b + ".");
答案 3 :(得分:0)
第一个建议:不要(永远!)声明两种完全相同的方法!!
public static String value() { //obtain *first and second* user input.
String sample = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE);
if (sample.isEmpty()) {
JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
sample = value();
}
return sample;
}
第二个建议:不要在value()方法和main()中检查输入的有效性:
public static int value() { //obtain second user input.
String sample = JOptionPane.showInputDialog(null, "Insert Value", "Enter amount ", JOptionPane.QUESTION_MESSAGE);
if (sample.isEmpty()) {
JOptionPane.showMessageDialog(null, "Error!", "No Value Detected", JOptionPane.ERROR_MESSAGE);
return value();
}
try {
int v = Integer.parseInt(sample);
return v;
} catch (NumberFormatException e) {
System.out.println("System detects that you are using characters");
return value();
}
}
对于第二种情况,主要成为:
public static void main(String[] args) {
int x1 = value();
int x2 = value(); // call again the *same* method
System.out.println("The total multiplication that you have inserted is "+x1 * x2+ ".");
}