我已经在这几个小时了,我有点沮丧。如果有人愿意看看我的代码并告诉我为什么我不能在StudentRecWithinput.Update()中使用Student1,我会很乐意伸出援助之手;
它在没有它的情况下工作,但它似乎只使用我正在阅读的最后一行数据。
MAIN
package Database;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Scanner Keyboard = new Scanner(System.in);
String choice1 = "";
System.out
.println("Would you like to update a students GPA? Please Input Yes or NO.");
choice1 = Keyboard.next();
if (choice1.charAt(0) == 'y' || choice1.charAt(0) == 'Y') {
recupdate();
} else {
System.out.println("Alright, Goodbye!");
}
}
public static void recupdate() throws FileNotFoundException {
Scanner Keyboard = new Scanner(System.in);
int choice2;
System.out.println("Here are the records!\n");
StudentRec Student1 = new StudentRec();
StudentRec Student2 = new StudentRec();
StudentRec Student3 = new StudentRec();
System.out
.println("Who's gpa will you be edditing? Please input (1, 2 or 3)");
choice2 = Keyboard.nextInt();
if (choice2 == 1) {
StudentRecWithInput.update(Student1);
}
if (choice2 == 2) {
StudentRecWithInput.update(Student2);
}
if (choice2 == 3) {
StudentRecWithInput.update(Student3);
}
}
}
STUDENTREC
package Database;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentRec {
// student related//
private String lastname;
private String firstname;
private String major;
private int age;
private static double gpa;
private static int credit;
// /non student related////
File info = new File("studentinfo.txt");
Scanner scan = new Scanner(info);
static int count = 0;
public StudentRec() throws FileNotFoundException {
{
count++;
if (count == 2) {
scan.nextLine();
}
else if (count == 3) {
scan.nextLine();
scan.nextLine();
}
firstname = scan.next();
lastname = scan.next();
age = scan.nextInt();
setGpa(scan.nextDouble());
major = scan.next();
setCredit(scan.nextInt());
System.out.println(firstname + " " + lastname + " " + age + " "
+ getGpa() + " " + major + " " + getCredit() + "");
}
}
public static int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
public static double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
}
STUDENTRECWITHINPUT
package Database;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentRecWithInput extends StudentRec {
static Scanner keyboard = new Scanner (System.in);
public StudentRecWithInput() throws FileNotFoundException {
super();
}
public static double update(int credit, double gpa)
{
double pastpoints = getCredit() * getGpa();
int newcredhours = keyboard.nextInt();
double newpoint = keyboard.nextDouble();
double semestergpa = newpoint / newcredhours;
double cumulate = (pastpoints + newpoint) / (newcredhours+ getCredit());
System.out.print(pastpoints);
return cumulate;
}
}
studentinfo.txt
Bob Bobbers 19 3.5 CPS 55
John Johners 20 3.7 BIO 70
Kat Katters 21 3.8 ITC 100
答案 0 :(得分:0)
StudentRecWithInput的签名
update(int credit, double gpa)
期望int和double,但是你用
调用它StudentRecWithInput.update(Student1);
您需要将签名更改为:
update(StudentRec student)
能够将学生信息传递给方法。
您还应该阅读有关静态和非静态方法之间差异的更多信息,因为现在您的代码没有编译更改。
答案 1 :(得分:0)
您可能想要调整策略。请考虑使用以下步骤:
所以你的主人可能有Vector<StudentRec>
当您需要更改StudentRec
时,update
类会有一个StudentRec
方法(无参数)。
确切的详细信息留给提问者练习,因为这似乎是一项学校任务。