当前位置: 首页 > 知识库问答 >
问题:

For循环未在函数中执行

梁宪
2023-03-14
public class Main {

    static Student[] studentList=new Student[100];
    int count=1;

    public void add(){
        Scanner sc = new Scanner(System.in);
        System.out.println("\nEnter student ID: ");
        String id = sc.next();
        Student st = forChecking(id);
        if(st==null){
            System.out.println("\nEnter the name of the student: ");
            String name=sc.next();
            System.out.println("\nEnter the program: ");
            String program=sc.next();
            System.out.println("\nEnter the total score: ");
            int total=sc.nextInt();

            studentList[count++]= new Student(id,name,program,total);
        }
        else{
            System.out.println("******STUDENT ALREADY EXIST******");
        }
        

    }

    public void edit(){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the ID of the student: ");
        String id=sc.next();
        Student st=forChecking(id);
        if(st==null){
            System.out.println("******NOT FOUND******");
        }
        else{
            System.out.println("----------Select an Item to Change----------");
            System.out.println("1. Name");
            System.out.println("2. Program");
            System.out.println("3. Total");
            System.out.println("4. Cancel");
            System.out.println("--------------------------------------------");
            System.out.println("Select Item (1-4): ");
            int choice=sc.nextInt();
            System.out.println("--------------------------------------------");
            switch(choice){
                case 1:
                    System.out.println("Enter new Name: ");
                    sc.nextLine();
                    st.setName(sc.nextLine());
                    System.out.println("******SUCCESSFULLY CHANGED******");
                    System.out.println("******RECORD UPDATED************");
                    break;
                case 2:
                    System.out.println("Enter new Program: ");
                    sc.nextLine();
                    st.setProgram(sc.nextLine());
                    System.out.println("******SUCCESSFULLY CHANGED******");
                    System.out.println("******RECORD UPDATED************");
                    break;
                case 3:
                    System.out.println("Enter new Total: ");
                    sc.nextLine();
                    st.setTotal(sc.nextInt());
                    System.out.println("******SUCCESSFULLY CHANGED******");
                    System.out.println("******RECORD UPDATED************");
                    break;
                case 4:
                    System.out.println("******CHOICE CANCELLED******");
                    break;
            }
        }

    }

    public void search() {
        int counter=0;
        Student[] student=new Student[count];
        System.out.println("\n----------Searching criteria----------");
        System.out.println("1. Search by ID");
        System.out.println("2. Search by name");
        System.out.println("3. Search by program");
        System.out.println("4. Cancel");
        System.out.println("--------------------------------------");
        System.out.println("Enter choice: ");
        Scanner sc = new Scanner(System.in);
        int search = sc.nextInt();
        switch(search){
            case 1:
                System.out.println("\nEnter the ID you want to search for : ");
                String searchID = sc.next();
                counter=0;
                for(int i = 0 ;i < this.count-1; i++){
                    Student st = studentList[i];
                    if(st!=null && st.getId().equalsIgnoreCase(searchID)){
                        student[counter++]=st;
                    }           
                }
                showResult(student,counter);
                break;
                
            case 2:
                counter=0;
                System.out.println("\nEnter the name you want to search for : ");
                String searchName = sc.next();                
                for(int i = 0 ;i < this.count-1; i++){
                Student st = studentList[i];
                    if(st!=null && st.getId().equalsIgnoreCase(searchName)){
                        student[counter++]=st;
                    }           
                }
                showResult(student,counter);
                break;
                
            case 3:
                counter=0;
                System.out.println("\nEnter the program you want to search for : ");
                String searchProgram = sc.next();                
                for(int i = 0 ;i < this.count-1; i++){
                Student st = studentList[i];
                    if(st!=null && st.getId().equalsIgnoreCase(searchProgram)){
                        student[counter++]=st;
                    }           
                }
                showResult(student,counter);
                break;
            case 4:
                System.out.println("******CHOICE CANCELLED******");
                break;
                
        }  
    }

    
    public void showResult(Student[] student, int counter){
        System.out.println("*****RECORD FOUND*****");
        for (int i = 0; i < counter; i++) {          
            System.out.println(student[i]);
        }

    }
    
    public Student forChecking(String id){
        for(Student student:studentList){
            if (student != null && student.getId().equalsIgnoreCase(id)) {
                return student;
            }
        }
        return null;
    }

    public static void main(String[] args) {

        while (true){
        Main initialization=new Main();

        System.out.println("\n----------Menu----------");
        System.out.println("1. Add Student");
        System.out.println("2. Edit Student Details");
        System.out.println("3. Search Student");
        System.out.println("4. Exit");
        System.out.println("-------------------------");
        System.out.print("Enter your choice: ");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        System.out.println("-------------------------");

        if(choice==1){
            initialization.add();
            }else if(choice==2){
                initialization.edit();
            }else if(choice==3){
                initialization.search();
            }else if(choice==4){
                System.out.println("Thank You For Using This System!");
                break;
            }else{
                System.out.println("******INVALID CHOICE******");
                System.out.println("******PLEASE TRY AGAIN******");
            }
        }
    }
}

class Student {
    private String id;
    private String name;
    private String program;
    private int total;

    public Student(String id, String name, String program, int total) {
        this.id = id;
        this.name = name;
        this.program = program;
        this.total = total;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    //method to calculate the cgpa
    public double cgpa(){
        return this.total/25;
    }

    @Override
    public String toString() {
        return "Student ID=" + id +
                "\nName=" + name  +
                "\nProgram=" + program  +
                "\nTotal=" + total +
                "\nCGPA=" + cgpa();
    }
}

共有1个答案

艾焕
2023-03-14

欢迎来到StackOverflow!这里发生的许多事情都促成了这一点,我将列举几个。但是,如果您以前没有使用过on,那么对于调试器来说,这是一个很好的用例。您的IDE(Eclipse、IntelliJ等)应该有一个调试器特性,我强烈建议您查看如何使用它,因为它将允许您逐步执行代码并了解正在发生的事情。在使用调试器的几个步骤中,我注意到了几个问题:

>

  • count初始化为1。这意味着您将第一个学生放在索引1中,因此索引0没有被占用。

    for循环迭代到count-1,而它应该一直迭代到count

  •  类似资料:
    • 本文向大家介绍C#中for循环、while循环循环执行的方法,包括了C#中for循环、while循环循环执行的方法的使用技巧和注意事项,需要的朋友参考一下 先给大家介绍下C#中的循环执行for循环 在这一节练习中,我们向大家介绍一下C#中的另一种重要的循环语句,for循环。 表达式1:一般为赋值表达式,给控制变量赋初值; 表达式2:逻辑表达式,循环控制条件;当条件为真时,循环执行循环体中的语句。

    • 问题内容: 我如何访问传递给fs.lstat函数的变量? 问题答案: 这是使用而不是for循环迭代值的完美理由。 另外,您可以使用@Aadit建议的闭包:

    • 我需要一个for循环中的click函数,这样每个id元素都可以单击。但是我还需要click函数中的I,这就是为什么我认为自动执行匿名函数是最好的方法。但出于某种原因,这不起作用,可能是因为单击函数不允许我转发参数?我做错了什么?

    • } 链接:https://www.hackerrank.com/challenges/java-string-compare/problem

    • 问题内容: 我有以下代码返回而不是循环内的每个值。 我需要怎么做才能获得循环值? 问题答案: 用 IIFE 封闭 **** 这样的话,价值将被保留,该次迭代的而不是将其设置为时间的最后一个值被称为回

    • 我想在一个并行外部循环中运行一个包含for循环(应该并行运行)的函数。因此看起来如下所示: 给定上面的代码,我希望在函数中为循环创建5个并行线程,并且希望这5个线程中的每个线程创建另一个线程来运行自己的并行for循环。