【发布时间】:2021-04-25 09:01:46
【问题描述】:
我必须使用一种方法(beginntMitA)。 不知何故,如果没有 main 方法,它就无法工作。 有谁知道我该如何解决?
public class StringTest{
public static void beginntMitA(String args[]){
String s = "java";
char ch1 = s.charAt(0);
char ch2 = s.charAt(1);
char ch3 = s.charAt(2);
char ch4 = s.charAt(3);
char ch5 = s.charAt(4);
System.out.println( ch1 );
System.out.println( ch2 );
System.out.println( ch3 );
System.out.println( ch4 );
System.out.println( ch5 );
}
}
【问题讨论】:
-
char ch5 = s.charAt(4);将抛出异常。您的字符串"java"只有 4 个字符,它们的索引为 0、1、2 和 3。