public class E { public static void main (String args[]) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; System.out.println(s3); } }如果控制台输入java E 0 1 2 3 4 5 6 7 8 9会输出什么,解释一下
时间: 2023-06-14 19:05:30 浏览: 122
根据代码,控制台输入的参数将被存储在字符串数组args[]中。在这个例子中,输入了10个参数,因此args数组的长度为10。根据Java中数组的下标从0开始的规则,args[1]将存储第一个参数"0",args[2]将存储第二个参数"1",args[3]将存储第三个参数"2",args[4]将存储第四个参数"3",以此类推。
在这个程序中,只有s3被输出,即args[3],因此控制台将输出"2"。
相关问题
public class Test{ public static void main(String[] args){ String s1 = "runoob"; String s2 = "runoob"; System.out.println("s1 == s2 is:" + s1 == s2);}}
The code you provided has a small mistake in the print statement. The expression `"s1 == s2 is:" + s1` is concatenated with `== s2` before being passed to `System.out.println()`. This causes a compilation error because you cannot concatenate a string with a boolean value directly.
To fix this, you need to use parentheses to group the concatenation expression properly. Here's the corrected code:
```java
public class Test {
public static void main(String[] args) {
String s1 = "runoob";
String s2 = "runoob";
System.out.println("s1 == s2 is: " + (s1 == s2));
}
}
```
In this corrected code, the expression `(s1 == s2)` is properly grouped and then concatenated with the string `"s1 == s2 is: "`.
public class d2 { public static void main(String[] args) { String s1=new String("pb_java_00p_T5"); String s2=s1.substring(s1.lastIndexOf(s1.lastIndexOf("_")); System.out.println("s2="+s2); } }
这段 Java 代码的运行结果会抛出 StringIndexOutOfBoundsException 异常。原因是在 s1.lastIndexOf("_") 返回的索引值为 9 时,调用 s1.lastIndexOf(s1.lastIndexOf("_")) 会得到 s1.charAt(9) 的 ASCII 码值 48('0'的 ASCII 码),而这个值大于了字符串 s1 的长度,因此导致数组越界异常。正确的代码应该是:
```
public class d2 {
public static void main(String[] args) {
String s1 = new String("pb_java_00p_T5");
String s2 = s1.substring(s1.lastIndexOf("_") + 1); // 修改此处
System.out.println("s2 = " + s2);
}
}
```
这样就可以得到正确的输出结果:s2 = T5。