LAPTOP-8KA88UT5_20200829-陈浩
问题
1.
class Monkey() {
public static void hello() {
System.out.println("hello,i am monkey");
}
}
public class MonkeyTest() {
public static void main(String[] args) {
Monkey m = null;
m.hello();
}
}
解决:
因为hello是类方法,所以在main方法中调用hello方法可以正常输出 hello,i am monkey ;如果hello是实例方法,则在执行 m.hello 方法时会抛出空指针异常,另外,不建议用实例去调用类方法。
2.求字符串的所有碎片平均长度
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = input.nextLine();
char[] ch1 = s.toCharArray();
double q = 1;
for (int i = 0; i < ch1.length - 1; i++) {
while (ch1[i] != ch1[i + 1]) {
q++;
break;
}
}
double avg = ch1.length / q;
System.out.println(avg);
}
吐槽
抄代码真**爽!!
近期评论