Simple student grade management system completed according to IDEA

Simple student grade management system completed according to IDEA

Requirement: Input option, when you choose to enter the grades, input the grades of each student, until the input (-1) ends to enter the grades, and finally output the grades of several students, and then loop to the main interface, enter other options, come Query the average score, the highest score and the lowest score of the student's grades, and then exit the system.

1. Main interface

insert image description here

System.out.println("----学生成绩管理系统----");
            System.out.println("1.录入成绩");
            System.out.println("2.平均分");
            System.out.println("3.最高分");
            System.out.println("4.最低分");
            System.out.println("5.退出系统");
            System.out.println("请选择:");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();

2. Entry grades

insert image description here

if (n == 1) {
    
    
                while (true) {
    
    
                    System.out.println("请输入学生成绩:");
                    int score = sc.nextInt();
                    if (score == -1) {
    
    
                        break;
                    }
                    max = Math.max(score, max);
                    min = Math.min(score, min);
                    sum = sum + score;
                    count++;
                }
                System.out.println("你一共输入了" + count + "个学生的成绩");
            }

3. Average score

insert image description here

if (n == 2) {
    
    
                int avg = sum == 0 ? 0 : sum / count;
                System.out.println("平均分" + avg);
            }

4, maximum minutes

insert image description here

if (n == 3) {
    
    
                System.out.println("最高分" + max);
            }

5. Minimum score

insert image description here

if (n == 4) {
    
    
                System.out.println("最低分" + min);
            }

6. Exit the system

insert image description here

if (n == 5) {
    
    
                System.out.println("谢谢使用,再见!");
                break;
            }

6. Summary

Because each output has to exit the loop, you need to use while.
Copy all the overall code here.

int count = 0;
        int max = -1;
        int min = 101;
        int sum = 0;
        while (true) {
    
    
            System.out.println("----学生成绩管理系统----");
            System.out.println("1.录入成绩");
            System.out.println("2.平均分");
            System.out.println("3.最高分");
            System.out.println("4.最低分");
            System.out.println("5.退出系统");
            System.out.println("请选择:");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            if (n == 1) {
    
    
                while (true) {
    
    
                    System.out.println("请输入学生成绩:");
                    int score = sc.nextInt();
                    if (score == -1) {
    
    
                        break;
                    }
                    max = Math.max(score, max);
                    min = Math.min(score, min);
                    sum = sum + score;
                    count++;
                }
                System.out.println("你一共输入了" + count + "个学生的成绩");
            }
            if (n == 2) {
    
    
                int avg = sum == 0 ? 0 : sum / count;
                System.out.println("平均分" + avg);
            }
            if (n == 3) {
    
    
                System.out.println("最高分" + max);
            }
            if (n == 4) {
    
    
                System.out.println("最低分" + min);
            }
            if (n == 5) {
    
    
                System.out.println("谢谢使用,再见!");
                break;
            }
        }

Problems with this system

If you directly select the average score, the highest score or the lowest score without entering the score, there will be a program error, or the value will be incorrect.
insert image description here
Due to the limited time this time, I will update you next time to sort out how to avoid this problem.

In addition, if you have any ideas, you can also comment below, and we can communicate and improve together! ! ! thank you all!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324171796&siteId=291194637