所以我还是Java的新手,自从我用Java编程以来,它已经有一段时间了。我想知道的是如何将一个整数(如学号)分配给数组列表中的元素。我的arraylist中有三个元素,每个元素必须有一个7位数的学生编号,第7位是顺序的下一个数字(例如,el 1,学生编号1234567,el 2,1234568,el 3与1234569)。我不确定我是否在正确的轨道上。此代码是一项正在进行的工作,我还有许多事情要添加或修复。我的arraylist位于名为StudentApp的下一个代码块中。
public class Student {
//fields
private String firstName;
private String lastName;
private int sNumber;
private String major;
private double gpa;
private static int count() {
int count = 1234567;
for (int i = 1; i <= count; i++) {
System.out.print(count);
}
return count;
}
//constructors
public Student() {
firstName = null;
lastName = null;
sNumber = 1234567;
major = null;
gpa = 0.0;
}
public Student(String fName, String lName, int sNumber, String maj, double gpa) {
firstName = fName;
lastName = lName;
sNumber = sNumber;
major = maj;
gpa = gpa;
}
//methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getsNumber() {
for(int sNumber = 0; sNumber >= 7; sNumber++) {
int sNumber = 1234567;
sNumber++
}
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return "S" + sNumber + " " + firstName + " "+ lastName + " " + "(" + major +")" + " " + "gpa:" + gpa;
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class StudentApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Student> names = new ArrayList<Student> ();
Student student1 = new Student();
student1.setFirstName("Brian");
student1.setLastName("Scholl");
student1.setMajor("CS");
student1.setGpa(3.5);
Student student2 = new Student();
student2.setFirstName("Emily");
student2.setLastName("Davies");
student2.setMajor("ME");
student2.setGpa(3.7);
Student student3 = new Student();
student3.setFirstName("Sarah");
student3.setLastName("Dixon");
student3.setMajor("EE");
student3.setGpa(3.8);
names.add(student1);
names.add(student2);
names.add(student3);
int choice;
do{
displayMenu();
choice = input.nextInt();
switch(choice) {
case 1:
Student student4 = new Student();
System.out.print("First name: ");
String fName1 = input.nextLine();
System.out.println();
System.out.print("Last name: ");
String lName1 = input.nextLine();
System.out.println();
System.out.print("Major: ");
String major1 = input.nextLine();
System.out.println();
System.out.print("GPA: ");
double gpa1 = input.nextDouble();
student4.setFirstName(fName1);
student4.setLastName(lName1);
student4.setMajor(major1);
student4.setGpa(gpa1);
names.add(student4);
break;
case 2:
System.out.print("Find student with sNumber S");
input.nextInt();
student1.getsNumber();
//statement on finding a student
break;
case 3:
//statement on deleting a student
break;
case 4:
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
break;
case 5:
names.size();
break;
case 6:
System.out.println("Good bye");
break;
default:
System.out.println("Invalid Selection");
System.out.println();
break;
}
System.out.println();
}while(choice != 0 );
}
public static void displayMenu() {
System.out.println("1. Add a student");
System.out.println("2. Find a student");
System.out.println("3. Delete a student");
System.out.println("4. Display all students");
System.out.println("5. Display the total number of students");
System.out.println("6. Exit");
}
}
答案 0 :(得分:2)
使用初始化值(例如1234567)将静态属性(studentNumberSequence)添加到Student类,并在构造函数中增加它的值。您可以在构造函数中添加以下行:
sNumber = studentNumberSequence++;
我建议您使用Map(例如HashMap)而不是ArrayList并应用&#39; sNumber&#39;作为地图键。
答案 1 :(得分:0)
没有理由按照你的方式尝试这样做。考虑一下,您可以简单地定义一个初始学号;根据你的代码,我认为这将是int studentNumber = 1234567;
。
现在,当您将学生添加到列表时,不要忽略添加该号码或初始化所有具有相同号码的学生;这只会使事情复杂化。
鉴于您现在拥有一个号码,请从用户那里获取其他信息,并添加一个新的Student
,其中包含您为其分配的学生编号。例如:
names.add(new Student(firstName, lastName, studentNumber++, major, gpa));
此外,您还有许多其他问题我不会在这里提出,但会向您发出警告。