Java:使用变量指定数组长度的编码?

标签 java arrays

我正在开发一个学生分数应用程序,该应用程序接受一个或多个学生的姓氏、名字和分数,并将结果存储在数组中。然后它按姓氏字母顺序打印学生及其分数。我们不知道有多少学生,但不会超过 100 人。

我们必须在学生信息的末尾显示类(class)平均分,并在每个成绩比类(class)平均分低 10 分以上的学生后面显示一条消息。

我的第一个问题是我创建了一个 do/while 循环来询问用户是否想输入另一个,但它不起作用!?!?

其次,我不知道如何向个别学生显示“低于 10 分”的消息。

public class Student implements Comparable
{
    String firstName;
    String lastName;
    int score; 

    //stores last name, first name and score for each student
    public Student(String lastName,String firstName,int score)
    {
        this.lastName = lastName;
        this.firstName = firstName;
        this.score = score;    
    }
    //implement the comparable interface so students can be sorted by name
    public int compareTo(Object o)
    {
        Student otherStudent = (Student)o;

        if(otherStudent.lastName.equals(lastName))
            {
            return firstName.compareToIgnoreCase(otherStudent.firstName);
            }
        else
            {
            return lastName.compareToIgnoreCase(otherStudent.lastName);
            }
    }
    public String toString()
    {
        return lastName + ", " + firstName + ": " + score; 
    }
}

import java.util.Scanner;
import java.util.Arrays;

public class StudentApp
{
    static Scanner sc = new Scanner(System.in);

    public static void main(String [] args)
    {
        Student [] studentArray;
        String lastName;
        String firstName;
        int score = 0;
        double average = 0;


        System.out.println("Welcome to the Student Scores Application.");
        System.out.println();

        do{

            //code that uses variable to specify the array length
        int nStudent = 100;  //array size not set unit run time
        studentArray = new Student[nStudent];

            for (int i=0; i<nStudent; i++)
            {
            System.out.println();

            lastName = Validator.getRequiredString(sc, 
                           "Student " + (i+1) +  " last name: ");
            firstName = Validator.getRequiredString(sc, 
                           "Student " +  " first name: ");               
            score = Validator.getInt(sc, 
                         "Student " + " score: ",
                        -1, 101);

            studentArray[i] = new Student(lastName, firstName, score);

            double sum = 0.0;
            sum += score;
            average = sum/nStudent;
            }
        }while (getAnotherStudent());

        Arrays.sort(studentArray);

        System.out.println();

        for (Student aStudent: studentArray)
        {
            System.out.println(aStudent);
            if (score<= (average-10))
            {
                System.out.println ("Score 10 points under average");
            }
        }
        System.out.println("Student Average:" +average);
    }
    public static boolean getAnotherStudent()
    {
        System.out.print("Another student? (y/n): " );
        String choice = sc.next();
        if (choice.equalsIgnoreCase("Y"))
            return true;
        else
            return false;
    }
}

最佳答案

这里有一些一些问题:

  • 每次执行 do...while 时,都会重新实例化 studentArraysum。这意味着当 getAnotherStudent() 为 true 时,您之前迭代的所有数据都会被破坏 - 您只想实例化数组并仅求和一次
  • 如果您的学生超过 100 名,您也不​​会停止。您的循环中也需要一个围绕 nStudent 的结束条件。
  • 您应该对 getAnotherStudent() 进行一些调整,以便可以阻止数据,并在输入有效数据时等待 - 通过使用循环:

     public static boolean getAnotherStudent() {
         Scanner sc = new Scanner(System.in);
         System.out.print("Another student? (y/n): " );
         if (sc.hasNext()) {  
             String choice = sc.next();
             // blocks here - ignores all input that isn't "y" or "n"
             while(!((choice.equalsIgnoreCase("Y") || choice.equalsIgnoreCase("N")))) {
                 if (choice.equalsIgnoreCase("Y")) {
                     return true;
                 }
                 System.out.print("Another student? (y/n): " );
                 choice = sc.next();
             }
          }
          return false; // obligatory
    

关于Java:使用变量指定数组长度的编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778707/

相关文章:

循环字符串数组中的C内存管理

C++:将CSV文件读入结构数组

java - 如何使用 Java Servlet 和 JSP 以及其他字段从同一页面上传两个文件?

java - Hibernate - 编写查询所需的帮助

java - Swing 是否支持 Windows 7 风格的文件选择器?

java - 基本的java : scanning files,数组

javascript - JS 中合并数组

java - java中获取正确的非虚拟MAC地址

java - 通过 SockJS 连接 Stomp 并订阅消息后仅接收 header

c - 将二维数组重新定义为另一个二维数组 C 的引用副本