Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Student和grades数组_Java_Arrays - Fatal编程技术网

java中的Student和grades数组

java中的Student和grades数组,java,arrays,Java,Arrays,我已经创建了两个数组。一个带学生姓名的字符串和一个带分数的整数。数据由JOptionPane插入。现在,如何显示成绩高于c的学生的姓名???您只需在成绩数组上迭代并检查条件: for (int i=0; i<grades.length; i++) { if(grades[i] > "C") //insert your function here } 但是,如果这两个参数密切相关,则应将它们包装到Student对象中 int key=9; int

我已经创建了两个数组。一个带学生姓名的字符串和一个带分数的整数。数据由JOptionPane插入。现在,如何显示成绩高于c的学生的姓名???

您只需在成绩数组上迭代并检查条件:

for (int i=0; i<grades.length; i++)
{
    if(grades[i] > "C") //insert your function here
}
但是,如果这两个参数密切相关,则应将它们包装到Student对象中

       int key=9;
       int a = 0;

       boolean found = false;   

       for ( a = 0; a < grades.length; a++)
       {
       if (grades[ a ]  == key );
       {
       found = true;      
       break;
       }
       }

       if (found)   
       {

       JOptionPane.showMessageDialog(null,"Is found " + key "in the index:" +a  );

       System.exit(0);
      } 

>我现在如何使-a-返回该字符串中的characterword,而不是像您所说的那样返回字符串位置,只保存值,而不是索引

int grade = grades[a];

虽然我需要再次强调,但这不是一个好的设计。Java是一种面向对象的语言,成绩应该是学生不可分割的一部分。此实现令人困惑,应按以下方式重新创建-声明一个新类:

public class Student{

    private String name;
    private int grade;

    //getters and setters to access private fields
    public String getName()
    {
         return this.name;
    }

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

    public int getGrade()
    {
         return this.grade;
    }

    public void setGrade(int grade)
    {
        this.grade = grade;
    }
}
然后,您只需声明一个学生对象数组,就可以对其进行迭代并有效地获取或修改任何学生数据:

ArrayList <Student> students = new ArrayList <Student> ();
students.add(new Student("John", 5);
students.add(new Student("Jane", 3);
students.add(new Student("Tim", 4);
for(Student s : students)
{
    //your operations here
}

你应该考虑创建一个有两个变量:名字和等级的班级学生。这将使操纵学生变得更容易,因为你只需要处理一个列表。这是你的家庭作业。我添加了一个答案,但为了将来:你不应该在这里问新问题,你应该把它作为新问题发布。
ArrayList <Student> students = new ArrayList <Student> ();
students.add(new Student("John", 5);
students.add(new Student("Jane", 3);
students.add(new Student("Tim", 4);
for(Student s : students)
{
    //your operations here
}