Java 将字符串中的 2 个整数相乘

Java Multiplying 2 integers from strings

我已经尝试搜索多个关于乘以 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+ ".");

不能将字符串相乘。您可以将 个数字 相乘,听起来这就是您想要做的。事实上,您已经将字符串解析为整数 - 然后忽略了结果。你只需要改变这个:

 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) + ".");

您正在尝试将两个字符串而不是两个数字相乘

你应该这样使用:

System.out.println("The total multiplication that you have inserted is "+ Integer.parseInt(sample) * Integer.parseInt(sample2)+ ".");

您必须将 Integer.parseInt() 的 return 值分配给变量。作为参数传递的变量保持不变。

 int a = Integer.parseInt(sample); 
 int b = Integer.parseInt(sample2);
 System.out.println("The total multiplication that you have inserted is " + a * b + ".");

第一个建议:不要(永远!)声明 2 个方法完全相同!!

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();
  }
}

对于第二种情况,main 变为:

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+ ".");
}