Java 我怎样才能让它下次运行? publicstaticvoidmain(字符串[]args){ 扫描仪s=新的扫描仪(System.in); 最终int学生人数=50; int i=0; char choice1='y'; 字符串[]stdntName=新字符串[50]; String[]WIDNUM=新字符串[STUDENT_SIZE]; int[]EXM1=新int[学生大小]; int[]EXM2=新int[学生大小]; int[]EXM3=新int[学生人数]; int[]finalExm=新int[学生人数]; 对于(i=0;i
这是错误消息: 是否要输入另一个?(是/否):线程“main”java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围:0 位于java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48) 位于java.base/java.lang.String.charAt(String.java:712) 在Proj4.main(Proj4.java:77)只需添加Java 我怎样才能让它下次运行? publicstaticvoidmain(字符串[]args){ 扫描仪s=新的扫描仪(System.in); 最终int学生人数=50; int i=0; char choice1='y'; 字符串[]stdntName=新字符串[50]; String[]WIDNUM=新字符串[STUDENT_SIZE]; int[]EXM1=新int[学生大小]; int[]EXM2=新int[学生大小]; int[]EXM3=新int[学生人数]; int[]finalExm=新int[学生人数]; 对于(i=0;i,java,arrays,loops,input,char,Java,Arrays,Loops,Input,Char,这是错误消息: 是否要输入另一个?(是/否):线程“main”java.lang.StringIndexOutOfBoundsException中的异常:字符串索引超出范围:0 位于java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48) 位于java.base/java.lang.String.charAt(String.java:712) 在Proj4.main(Proj4.java:77)只需添加s.nextLine(),因
s.nextLine()
,因为您的扫描仪没有到达获取选项的点1
您的代码已修复
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
final int STUDENT_SIZE = 50;
int i = 0;
char choice1 = 'y';
String [] stdntName = new String [50];
String [] WIDNUM = new String [STUDENT_SIZE];
int [] EXM1 = new int [STUDENT_SIZE];
int [] EXM2 = new int [STUDENT_SIZE];
int [] EXM3 = new int [STUDENT_SIZE];
int [] finalExm = new int [STUDENT_SIZE];
for (i=0; i < stdntName.length; i++) {
System.out.print("Please enter the name of Student " + (i+1) + ": ");
stdntName[i] = s.nextLine();
System.out.println("Please enter the WID of Student " + (i+1) + ": ");
WIDNUM[i] = s.nextLine();
if (WIDNUM[i].length() != 9 ) {
System.out.println("**Invalid WID...must be 9-digits");
System.out.println("Please re-enter score:");
WIDNUM[i] = s.nextLine();
}
System.out.println("Please enter score for Exam 1: ");
EXM1[i] = s.nextInt();
if (EXM1[i] < 0 || EXM1[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM1[i] = s.nextInt();
}
System.out.println("Please enter score for Exam 2 ");
EXM2[i] = s.nextInt();
if (EXM2[i] < 0 || EXM2[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM2[i] = s.nextInt();
}
System.out.println("Please enter score for Exam 3 ");
EXM3[i] = s.nextInt();
if (EXM3[i] < 0 || EXM3[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM3[i] = s.nextInt();
}
System.out.println("Please enter score for Final Exam ");
finalExm[i] = s.nextInt();
if (finalExm[i] < 0 || finalExm[i] > 100) {
System.out.println("**Invalid score...please enter 0-100 only");
System.out.println("Please re-enter score: ");
finalExm[i] = s.nextInt();
}
System.out.print("Do you wish to enter another? (y/n): ");
choice1 = s.nextLine().toLowerCase().charAt(0); // read entire line
if (choice1 != 'y') {
break;
}
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪s=新的扫描仪(System.in);
最终int学生人数=50;
int i=0;
char choice1='y';
字符串[]stdntName=新字符串[50];
String[]WIDNUM=新字符串[STUDENT_SIZE];
int[]EXM1=新int[学生大小];
int[]EXM2=新int[学生大小];
int[]EXM3=新int[学生人数];
int[]finalExm=新int[学生人数];
对于(i=0;i50){
System.out.println(“**无效分数…请仅输入0-50”);
System.out.println(“请重新输入分数:”);
EXM1[i]=s.nextInt();
}
System.out.println(“请输入考试2的分数”);
EXM2[i]=s.nextInt();
如果(EXM2[i]<0 | | EXM2[i]>50){
System.out.println(“**无效分数…请仅输入0-50”);
System.out.println(“请重新输入分数:”);
EXM2[i]=s.nextInt();
}
System.out.println(“请输入考试3的分数”);
EXM3[i]=s.nextInt();
如果(EXM3[i]<0 | | EXM3[i]>50){
System.out.println(“**无效分数…请仅输入0-50”);
System.out.println(“请重新输入分数:”);
EXM3[i]=s.nextInt();
}
System.out.println(“请输入期末考试分数”);
finalExm[i]=s.nextInt();
if(finalExm[i]<0 | | finalExm[i]>100){
System.out.println(“**无效分数…请仅输入0-100”);
System.out.println(“请重新输入分数:”);
finalExm[i]=s.nextInt();
}
s、 nextLine();
System.out.print(“是否要输入另一个?(y/n):”;
choice1=s.nextLine().toLowerCase().charAt(0);
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
final int STUDENT_SIZE = 50;
int i = 0;
char choice1 = 'y';
String [] stdntName = new String [50];
String [] WIDNUM = new String [STUDENT_SIZE];
int [] EXM1 = new int [STUDENT_SIZE];
int [] EXM2 = new int [STUDENT_SIZE];
int [] EXM3 = new int [STUDENT_SIZE];
int [] finalExm = new int [STUDENT_SIZE];
for (i=0; i < stdntName.length; i++) {
System.out.print("Please enter the name of Student " + (i+1) + ": ");
stdntName[i] = s.nextLine();
System.out.println("Please enter the WID of Student " + (i+1) + ": ");
WIDNUM[i] = s.nextLine();
if (WIDNUM[i].length() != 9 ) {
System.out.println("**Invalid WID...must be 9-digits");
System.out.println("Please re-enter score:");
WIDNUM[i] = s.nextLine();
}
System.out.println("Please enter score for Exam 1: ");
EXM1[i] = s.nextInt();
if (EXM1[i] < 0 || EXM1[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM1[i] = s.nextInt();
}
System.out.println("Please enter score for Exam 2 ");
EXM2[i] = s.nextInt();
if (EXM2[i] < 0 || EXM2[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM2[i] = s.nextInt();
}
System.out.println("Please enter score for Exam 3 ");
EXM3[i] = s.nextInt();
if (EXM3[i] < 0 || EXM3[i] > 50) {
System.out.println("**Invalid score...please enter 0-50 only");
System.out.println("Please re-enter score: ");
EXM3[i] = s.nextInt();
}
System.out.println("Please enter score for Final Exam ");
finalExm[i] = s.nextInt();
if (finalExm[i] < 0 || finalExm[i] > 100) {
System.out.println("**Invalid score...please enter 0-100 only");
System.out.println("Please re-enter score: ");
finalExm[i] = s.nextInt();
}
s.nextLine();
System.out.print("Do you wish to enter another? (y/n): ");
choice1 = s.nextLine().toLowerCase().charAt(0); // read entire line
if (choice1 != 'y') {
break;
}
}
}
}