我怎么能让这个运行另一个时间?


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;
}

}
}  
}

这是错误信息:

要输入另一个吗?(y/n):线程"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(),因为您的扫描仪没有到达获取choice1的点

你的代码fixed

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;
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新