我想要结果 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) );
}
}
如果您希望对整数进行算术运算,则需要告诉代码解析这些值。目前它只是使用字符串连接运算符,因为两个操作数 (str1
and str2
) 是字符串表达式。
尝试这个:
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”的东西而不是数字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)