java - 无法调用正确的数据

标签 java object filereader

我已经研究了几个小时了,我有点沮丧。如果有人愿意看一下我的代码并告诉我为什么不能在 StudentRecWithinput.Update(); 中使用 Student1,我会很乐意伸出援手;

它可以在没有它的情况下工作,但它似乎只使用我正在读入的最后一行数据。

主要内容

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);

    }
}
}

学生记录

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;
}

}

学生记录输入

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;

}

}

学生信息.txt

Bob Bobbers 19 3.5 CPS 55 
John Johners 20 3.7 BIO 70
Kat Katters 21 3.8 ITC 100

最佳答案

StudentRecWithInput 的签名

update(int credit, double gpa) 

期望 int 和 double,但你正在调用它

StudentRecWithInput.update(Student1);

您需要将签名更改为:

update(StudentRec student) 

能够将学生信息传递到该方法中。

您还应该详细了解静态方法和非静态方法之间的区别,因为现在您的代码没有编译变化。

关于java - 无法调用正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21947764/

相关文章:

javascript - 排序 JSON 提要

javascript - 在 JavaScript 中将 CSV 记录解析为对象数组

java - Base64转图像文件

java - Java中的getter/setter

java - 找到...对象,但使用 Java 堆栈预期出现 myClass 错误

python - 在 Python 中,两个对象何时相同?

javascript - 如何在浏览器中使用JavaScript从<input type ="file"/>获取curl -T --upload-data选项的等效数据格式?

java - LinkedBinaryTree<E> 类的 toString() 方法

java - 尝试围绕一个点旋转,但无论如何都会失去精度

java - Java中的getView和Beacon(Estimote)问题