【问题标题】:How can I get this to run another time? [duplicate]我怎样才能让它再次运行? [复制]
【发布时间】:2021-05-29 04:51:08
【问题描述】:
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)

【问题讨论】:

    标签: java arrays loops input char


    【解决方案1】:

    只需添加 s.nextLine(),因为您的扫描仪没有达到获取您的 choice1 的目的

    您的代码已修复

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

    【讨论】:

    • 哇,我感觉好笨哈哈谢谢!
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2022-01-16
    • 2011-03-12
    相关资源
    最近更新 更多