package com.beijing.javase;
import java.util.Scanner;
public class AB {
static int b;
public static void main(String[] args) {
AB u =new AB();
Scanner s =new Scanner(System.in);
int a =s.nextInt(9999)+1;
b =a;
int c,d=0;
for(int i =1;a>=3;i++) {
c = a/3;
d = c+a%3;
u.M(d);
a=d;
}
System.out.print(b);
}
public int M(int d) {
b+=d;
return d;
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9999
at java.lang.String.substring(Unknown Source)
at java.util.Scanner.buildIntegerPatternString(Unknown Source)
at java.util.Scanner.integerPattern(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at com.beijing.javase.AB.main(AB.java:8)
你这个nextInt(9999)意味着你会获取输入的9999位传给变量a
第一,你没有在下次输入中输入9999位的数据,第二,就算你输入了这么多位的数据,变量a也会放不下这么多数据
建议给代码加上高亮,博问支持 markdown 代码高亮语法
– dudu 5年前