我已经手工创建了我班级“学生”中的所有对象,并且只需要使用数组来输入名称,成绩和ID号。当我尝试时,它给我一个关于不兼容类型的错误。我理解这可能是由于对某种语法类型的错误的限制。有人可以帮助这部分的物理语法吗?非常感谢!
package bilak_sackin_assignment_7;
public class Bilak_Sackin_Assignment_7 {
public static void main(String[] args) {
student Frank = new student("Frank", 95.2, 11231);
student Billy = new student("Billy", 65.6, 656454);
student Alex = new student("Alex", 65.2, 123456);
student Miranda = new student("Miranda", 80.0, 963852);
student Joel = new student("Joel", 89.9, 486248);
Frank.display();
Billy.display();
Alex.display();
Miranda.display();
Joel.display();
}
}
class student {
String[] name = new String[5];
double[] grade = new double[5];
int[] ID = new int[5];
public student(String[] n, double[] g, int[] d) {
name = n;
grade = g;
ID = d;
}
public void display() {
for (int i = 0; i < 4; i++) {
System.out.println("Names: " + name[i]);
System.out.println("Grades: " + grade[i]);
System.out.println("Student ID: " + ID[i]);
}
}
}
答案 0 :(得分:1)
我对此也很陌生,但你肯定不需要数组,你需要创建对象。删除所有[和],像这样 -
public static void main(String[] args) {
student Frank = new student("Frank",95.2,11231);
student Billy = new student("Billy", 65.6, 656454);
student Alex = new student("Alex", 65.2, 123456);
student Miranda = new student("Miranda", 80.0, 963852);
student Joel = new student("Joel", 89.9, 486248);
Frank.display();
Billy.display();
Alex.display();
Miranda.display();
Joel.display();
}
}
class student
{
String name;
double grade;
int ID;
public student(String n, double g, int d)
{
name = n;
grade = g;
ID = d;
}
public void display()
{
System.out.println("Names: " + name);
System.out.println("Grades: " + grade);
System.out.println("Student ID: " + ID);
}
}
这适合我。
如果我是对的,请将我标记为正确,我需要代表! : - )
答案 1 :(得分:0)
public student(String[] n, double[] g, int[] d)
{
name = n;
grade = g;
ID = d;
}
您的构造函数需要一个字符串数组,一个双精度数组和一个整数数组。
然而,这是你传递的内容:
new student("Joel", 89.9, 486248);
一个String,一个double和一个int。
我的猜测,除了成绩之外,你的Student课程中不需要数组。
因此,第一步是删除name和id的数组。
第二步:传递一系列双打作为成绩。
答案 2 :(得分:0)
在student的构造函数中,所有输入都是原始数组,但是,在创建上述对象时,您没有使用它们。
如果您想使用构造函数,如下所示:
Student billy = new Student("Billy", 23, 35);
然后你必须用非数组输入定义构造函数,即
class Student {
String name;
double grade;
int id;
public Student(String name, double grade, int id) {
this.name = name;
this.grade = grade;
this.id = id;
}
}
答案 3 :(得分:0)
好吧,我决定使用@ NickL的想法,使用数组构造对象,然后将其作为数组访问。
包bilak_sackin_assignment_7;
公共课Bilak_Sackin_Assignment_7 {
public static void main(String[] args) {
String name[] = {"Frank", "Billy","Alex", "Miranda", "Joel" };
double grades[] = {95.2, 65.6, 65.2, 80.0, 89.9};
int iD[] = {112312, 656454, 123456, 963852, 486248};
for (int i = 0; i < 5; i++)
{
String names = name[i];
double grade =(grades[i]);
int ID = (iD[i]);
System.out.println("Name: " + names + " | Grade: " + grade + " | ID number: " + ID);
}
}
}