請幫助我。我是編程新手。我有一些任務要提交。 我使用Netbeans。我想編輯一個關於給定基數的學生的具體數據。首先,系統會提示學生的基準號碼。如果找到,系統將詢問用戶要編輯的數據(名稱或基準或年齡或標記)。用戶將指定哪些數據,然後系統會要求用戶輸入數據的新值。用新值設置數據後,系統將顯示關於學生的所有數據。 這裏是我的代碼:如何編輯arraylist中的對象內的每個變量
類:
公共類學生{所有的
int matric;
String name;
int age;
double mark;
String grade;
Student(int m, String n, int a, double mk) {
matric = m;
name = n;
age = a;
mark = mk;
}
public String computeGrade() {
return "";
}
public String getName() {
return name;
}
public void setName(int name) {
this.matric = name;
}
public int getMatric() {
return matric;
}
public void setMatric(int matric) {
this.matric = matric;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
else if (choice == 4) {
System.out.println ("Edit Student Data");
System.out.print("Enter the matric number>");
int matric = read.nextInt();
boolean found=false;
for (int i = 0; i < index; i++) {
if (myStud[i].getMatric() == matric) {
System.out.println("======================================");
System.out.println("Student Name = " + myStud[i].getName());
System.out.println("Student Matric = " + myStud[i].getMatric());
System.out.println("Student Age = " + myStud[i].getAge());
System.out.println("Student Mark = " + myStud[i].getMark());
System.out.println("Student Grade = " + myStud[i].computeGrade());
System.out.println("======================================");
found=true;
System.out.println("1. Edit Name");
System.out.println("2. Edit Matric");
System.out.println("3. Edit Age");
System.out.println("4. Edit Mark");
System.out.print ("Please choose data to edit > ");
int edit = read.nextInt();
if(edit == 1){
System.out.println("Enter Your Name ");
String name = read.next();
}
else if(edit == 2){
System.out.println("Enter Your Matric ");
matric = read.nextInt();
}
else if(edit == 3){
System.out.println("Enter Your Age ");
int age = read.nextInt();
// **how to save and replace the oldest?**
}
else if(edit == 4){
System.out.println("Enter Your Mark ");
int mark = read.nextInt();
}
System.out.println("Student Data has been updated!");
break;
}
}
if (found==false)
System.out.println("Student Data not Found!");
}
這段代碼不會編譯 - 如果沒有'if'或者甚至是一個方法,你會引入一個'else'的術語! – 2014-10-30 06:59:22
這只是if語句的一部分。這 – 2014-10-30 07:02:53
還有很多其他的代碼所缺少的,我不知道是什麼'**如何保存和更換最古老的?**'手段,爲什麼不'myStud [I] .setAge(年齡)'工作? – 2014-10-30 07:07:53