0

我正在学习 Java 入门课程,并且在从用户输入中提取数据并使用它根据用户输入量创建一组成绩时遇到问题。然后我假设从最高到最低组织该组数​​据并将其平均。到目前为止,这是我的代码

    //import Scanner

import java.util.Scanner;

public class Looper {


  public static void main(String[] args) {

    //introduce integers
    int highest = 0;
    int lowest = 0;
    int count = 0;
    int grade = (int) (Math.random() * 100);
    int student;

    int total = 0;
    double average = 0;
    //introduce Scanner 
    Scanner input = new Scanner(System.in);

    //printout number of students
    System.out.println("Enter number of students");
    student = input.nextInt();
    //narrow amount of students 
    if (student <= 10) {
      System.out.println("Enter the grades of the students");
    }

    else if (student >= 11) {
      System.out.println("Too many Students");
    }
    //close off 0               
    while (student != 0) {
      System.out.println("Didn't enter students");
    }

    grade = input.nextInt();
    //connect student and grade         
    for (grade = 0; grade <= 100; grade++) {
      student = grade;
      if (grade > 100) {
        System.out.println("Must be between 0 and 100")
      } if (grade <= highest) {
        grade++;
      }
    }

    for (grade = 0; grade >= 0; grade++) {
      student = grade;
      if (grade < -1) {
        System.out.println(" Must be between 0 and 100");
      }
      else if (grade >= lowest) {
        grade++;
      }
      //   form total and average
      total = total + grade;
      grade++;
      {
        average = (double) total / grade;
        //printout highest, lowest, and average
        System.out.println("The highest is" + highest + "\n The lowest is" + lowest + "The     average is\n" + average);
      }
    }
  }
}
4

1 回答 1

1

首先,使用不带括号的 if 会降低代码的可读性。其次,;缺少 afterSystem.out.println("Must be between 0 and 100")并且你有一个;after 你的 while 循环:while (Student!= 0);{

您的变量都以大写字母开头,而它们应该以小写字母开头。

此行之后有一个双;精度:int Grade= (int) (Math.random() *100);;

您代码中的注释也没有任何意义......

//introduce scanner

//introduce integers
int highest = 0;
int lowest=0; 
int count=0;
int Grade= (int) (Math.random() *100);; 
int Student;

int Total=0;
double average= 0;
//print out number of students

Scanner input = new Scanner(System.in);

扫描仪的介绍远低于您的评论,您不打印学生人数而是输入学生人数...

对不起,这段代码很乱,我什至不知道从哪里开始。

编辑:我对您的原始代码进行了一些重构,这应该使您走上正轨。

import java.util.Scanner;

public class Looper {

  public static void main(String[] args) {
    int highest = 100;
    int lowest = 0;
    int grade = 0;
    int students = 0;
    float total = 0;
    double average = 0;

    Scanner input = new Scanner(System.in);

    System.out.println("Enter number of students: ");
    students = input.nextInt();

    if (students <= 10) {
      System.out.println("Enter the grades of the students: ");



      // ADDED CODE
      for(int i = 0; i < students; i++) {
        do {
          grade = input.nextInt();

          if(grade >= 0 && grade <= 100) {
            System.out.println("Grade must be between 0 and 100");
          }
        } while(grade < 0 || grade > 100);

        if(grade > highest) {
          highest = grade;
        }
        if(grade < lowest) {
          lowest = grade;
        }

        total += grade;
      }

      average = (total/students);

      System.out.println("The highest is " + highest);
      System.out.println("The lowest is " + lowest);
      System.out.println("The average is " + average);
      // END ADDED CODE


    } else if (students >= 11) {
      System.out.println("Too many Students");
    }
  }
}
于 2013-07-03T11:29:49.630 回答