【问题标题】:Is there a reason my program ends before running my for loop? [closed]我的程序在运行我的 for 循环之前结束有什么原因吗? [关闭]
【发布时间】:2021-06-21 14:39:55
【问题描述】:

在我的编程课上,我被要求制作一份录取学生名单,每个学生都包含其中的信息,并显示所有录取学生的平均成绩以及他们是否被学校录取。一切正常,除了当我在程序结束时进入我的 for 循环以显示每个学生的信息及其平均值时,程序只是在显示学生的任何信息之前结束并且没有给出错误消息。

请帮忙!!

主类:

public class MainStudent {
 
public static void main(String[] args) {
    // TODO Auto-generated method stub

    
    Scanner scan = new Scanner(System.in);
    List<Student> students = new ArrayList<Student>();
    
    String FullName;
    String answer;
    
    System.out.println("Welcome! Please enroll your students");
    System.out.println("");
    
    
    do {
        Student student = new Student();
        
        
        System.out.println("Please enter the first name of the student");
        student.setFirstName(scan.next());
        
        System.out.println("");
        
        System.out.println("Please enter the last name of the student");
        student.setLastName(scan.next());
        
        System.out.println("");
        
        FullName = student.getFirstName() + " " +  student.getLastName();
        Boolean validation = false;
        
        while (validation == false) {
            System.out.println("Please enter the ID of " + FullName);
            
            try {
            student.setID(scan.nextInt());
            
            validation = true;
            
        }
        
        catch(Exception e) {
            
            System.out.println("please enter only numbers");
            System.out.println("");
            validation = false;
            scan.next();
        }
        
        }
        System.out.println("");
        
        System.out.println("Please enter the gender of " + FullName);
        student.setGender(scan.next());
        
        System.out.println("");
        
        
        System.out.println("Please enter the age of " + FullName);
        student.setAge(scan.nextInt());
        
        if (student.getAge() > 17) {
            
        do {
            System.out.println("");
            System.out.println("Please enter the first score of " + FullName);
            try {
                student.setScore1(scan.nextInt());
                if (student.getScore1() > 100 || student.getScore1() < 0) {
                    System.out.println("please enter a positive number below 100");
                    validation = false;
                    
                }
                else {
                    validation = true;
                    
                }
            }
            
            
            catch (Exception e) {
                
                System.out.println("Please only enter numbers");
                validation = false;
                scan.next();
            }
            
        }
        while (validation == false);
        
        System.out.println("");
        
        do {
            System.out.println("Please enter the second score of " + FullName);
            try {
                student.setScore2(scan.nextInt());
                
                if (student.getScore2() > 100 || student.getScore2() < 0) {
                    System.out.println("please enter a positive number below 100");
                    validation = false;
                    
                }
                else {
                    validation = true;
                    
                }
            }
            
            
            catch (Exception e) {
                
                System.out.println("Please only enter numbers");
                validation = false;
                scan.next();
            }
            
        }
        while (validation == false);
        
        System.out.println("");
        
        do {
            System.out.println("Please enter the third score of " + FullName);
            try {
                student.setScore3(scan.nextInt());
                if (student.getScore3() > 100 || student.getScore3() < 0) {
                    System.out.println("please enter a positive number below 100");
                    validation = false;
                    
                }
                else {
                    validation = true;
                    
                }
            }
            
            
            catch (Exception e) {
                
                System.out.println("Please only enter numbers");
                validation = false;
                scan.next();
            }
            
        }
        while (validation == false);

        
        System.out.println("");
        
        do {
            System.out.println("Please enter the fourth score of " + FullName);
            try {
                student.setScore4(scan.nextInt());
                if (student.getScore4() > 100 || student.getScore4() < 0) {
                    System.out.println("please enter a positive number below 100");
                    validation = false;
                    
                }
                else {
                    validation = true;
                    
                }
            }
            
            
            catch (Exception e) {
                
                System.out.println("Please only enter numbers");
                validation = false;
                scan.next();
            }
             
        }
        while (validation == false);
    }
        
        
        
        
        
        else {
            System.out.println("You can not enroll a student under the age of 18");
            
            student.setScore1(0);
            student.setScore2(0);
            student.setScore3(0);
            student.setScore4(0);
        }
        
        
        System.out.println("Do you want to register another student? \n Answer with Y/N");
         answer = scan.next();
         
        
        
        }
        while (answer.equals("Y") || answer.equals("y"));
        

    
        
            for (int i = 0; i < students.size(); i++) {
        System.out.println("");
        System.out.println("*******************************");
        System.out.println("Name: " + students.get(i).getFirstName());
        System.out.println("Age: " + students.get(i).getAge() );
        System.out.println("Gender: " + students.get(i).getGender());
        System.out.println("Scored Average: " + students.get(i).Average(i, i, i, i));
        System.out.println(students.get(i).Enrolled(i));
        System.out.println("*******************************");
            }
        
        
        

    scan.close();
}

}

【问题讨论】:

  • 请在您的帖子中正确应用文本/代码格式选项。

标签: java list for-loop arraylist


【解决方案1】:

循环不会运行,因为您实际上并未将任何内容添加到名为“students”的数组列表中。您可能希望开始在 do-while 循环内的该列表中添加内容,以便 students.size() 不等于 0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2014-05-12
    相关资源
    最近更新 更多