如何将两个字符串转换为整数以使数学正确?

发布于 2024-09-30 23:03:17 字数 227 浏览 4 评论 0原文

我想要结果 no:5 但我得到 no:23

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

  System.out.println("Result:" + (str1+str2) );
  }
}

I want the result no:5 but I get no:23

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

  System.out.println("Result:" + (str1+str2) );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

在巴黎塔顶看东京樱花 2024-10-07 23:03:17

如果您希望对整数进行算术运算,则需要告诉代码解析这些值。目前它仅使用字符串连接运算符,因为两个操作数(str1str2)都是字符串表达式。

尝试一下:

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

    int num1 = Integer.parseInt(str1);
    int num2 = Integer.parseInt(str2);

    System.out.println("Result:" + (num1 + num2) );
  }
}

请注意,当您使用“真实”数据(而不是此处肯定有效的硬编码值)时,Integer.parseInt 将抛出 NumberFormatException如果你给它类似“x”的东西而不是数字。

If you want arithmetic to be done on integers, you need to tell your code to parse the values. Currently it's just using the string concatenation operator, because both of the operands (str1 and str2) are string expressions.

Try this:

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

    int num1 = Integer.parseInt(str1);
    int num2 = Integer.parseInt(str2);

    System.out.println("Result:" + (num1 + num2) );
  }
}

Note that when you're using "real" data (instead of hard-coded values which will definitely be valid here), Integer.parseInt will throw a NumberFormatException if you give it something like "x" instead of a number.

眼趣 2024-10-07 23:03:17

str1str2String 对象。 + 运算是为 String 对象定义的,其工作方式类似于这些字符串的串联:

"one" + "two" -> "onetwo"
"1" + "2" -> "12"

如果您需要算术 + 运算,那么您需要数字类型(int、float、...)。在这种情况下,您必须将字符串解析为数值,例如:

String str1 = "2";
int int1 = Integer.parseInt(str1);  // int1 is now 2

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

    // `+` operation on Strings
    System.out.println("Concatenation:" + (str1+str2) );

    // `+` operation on integers
    System.out.println("Addition:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) );
  }
}

str1 and str2 are String objects. The + operation is defined for String objects and works like a concatenation of those Strings:

"one" + "two" -> "onetwo"
"1" + "2" -> "12"

If you need an arithmetic + operation, then you need numeric types (int, float, ...). In you're case, you'll have to parse the Strings to numeric values, like:

String str1 = "2";
int int1 = Integer.parseInt(str1);  // int1 is now 2

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

    // `+` operation on Strings
    System.out.println("Concatenation:" + (str1+str2) );

    // `+` operation on integers
    System.out.println("Addition:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) );
  }
}
寂寞清仓 2024-10-07 23:03:17

您需要使用 parseInt()。 Java 中使用“+”运算符连接两个字符串以及添加两个数字。因此,您必须将字符串转换为整数才能使用“+”运算符将它们相加。

编辑:

还有 parseFloat()parseDouble() 如果您使用的是十进制数字

You need to use parseInt(). The "+" operator is used in Java to concatenate two strings as well as to add two numbers. So you have to convert the strings to an integer to be able to add them with the "+" operator.

EDIT:

There is also parseFloat() and parseDouble() if you are working with decimal numbers

红墙和绿瓦 2024-10-07 23:03:17

这个怎么样?

class Assignment3 
{ 
  public static void main(String args[]) 
  { 
    String str1 = "2"; 
    String str2 = "3"; 

    System.out.println("Result:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) ); 
  } 
}

How about this?

class Assignment3 
{ 
  public static void main(String args[]) 
  { 
    String str1 = "2"; 
    String str2 = "3"; 

    System.out.println("Result:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) ); 
  } 
}
海未深 2024-10-07 23:03:17

您将数字字符串视为文本字符串,但实际上您需要将字符串解析为整数。

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

  System.out.println("Result:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) );
  }
}

You are treating numerical strings as textual strings, but actually you need to parse the strings to Integer.

public class Assignment3
{
  public static void main(String args[])
  {
    String str1 = "2";
    String str2 = "3";

  System.out.println("Result:" + (Integer.parseInt(str1)+Integer.parseInt(str2)) );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文