Java - 用户输入不存储

Java - User input do not store

public class Student{

    String name;
    double cgpa;
    String status;

    public void setName(String name){

        this.name=name;
    }

    public String getName(){

        return name;
    }
    public void setCgpa(double cgpa){

        this.cgpa=cgpa;
    }

    public double getCgpa(){
        return cgpa;
    }

    public String findStatus(){


        if(cgpa >= 2.0){
            System.out.println("Your Status : Good Standing (GS)");
        } else if (cgpa >1.7){
            System.out.println("Your Status: Probational (P)");
        } else{
            System.out.println("Your Status: Fail (F)");
        }
        return status;
    }

    public String toString() {

        return "\nStudent Name: "+name+"\nYour current status: "+status;
    }

    public static void main(String[]args){

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your name: ");
        String name=in.nextLine();
        System.out.println("Please enter your CGPA: ");
        double cgpa=in.nextDouble();

        Student mystudent = new Student();
        System.out.println("Your name: " +mystudent.getName());
        System.out.println("Your CGPA: " +mystudent.findStatus());

    }       
}

以下代码没有任何语法错误,但是当我尝试执行它时,用户输入不存储,结果输出为空。我尝试在 TestStudent class 中创建 运行 但结果仍然相同。

下面的代码不会存储你的信息,因为你没有保存它。

Student mystudent = new Student();
System.out.println("Your name: " +mystudent.getName());
System.out.println("Your CGPA: " +mystudent.findStatus());

使用下面的代码来做到这一点:

Student mystudent = new Student();
mystudent.setName(name);
mystudent.setCgpa(cgpa);

// now it will give you the result
System.out.println("Your name: " +mystudent.getName());
System.out.println("Your CGPA: " +mystudent.findStatus());

您刚刚忘记存储数据:

您需要添加以下行:

 mystudent.setName(name);
 mystudent.setCgpa(cgpa);  

看起来像这样。

public static void main(String[]args){
    Student mystudent = new Student();

    Scanner in = new Scanner(System.in);

    System.out.println("Please enter your name: ");
    String name=in.nextLine();
    mystudent.setName(name);

    System.out.println("Please enter your CGPA: ");
    double cgpa=in.nextDouble();
    mystudent.setCgpa(cgpa); 


    System.out.println("Your name: " +mystudent.getName());
    System.out.println("Your CGPA: " +mystudent.findStatus());

}       

这段代码中有两个错误的实现。您的 findStatus() 返回空(未初始化)字符串并且您没有设置(初始化)namecgpa 这是编辑后的代码

public class Student{

String name; double cgpa; String status;

public void setName(String name){

this.name=name; }

public String getName(){

return name; } public void setCgpa(double cgpa){

this.cgpa=cgpa; }

public double getCgpa(){ return cgpa; }

public String findStatus(){

if(cgpa >= 2.0){
    status = "Status : Good Standing (GS)";
} else if (cgpa > 1.7){
     status = "Your Status: Probational (P)";
} else{
     status = "Status: Fail (F)";
}
return status;
} public String toString(){
  return "\nStudent Name: "+name+"\nYour current status: "+status;    }

public static void main(String[]args){

Student mystudent = new Student();

Scanner in = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name=in.nextLine();
mystudent.setName(name);

System.out.print("Please enter your CGPA: ");
double cgpa=in.nextDouble();
mystudent.setCgpa(cgpa);

System.out.println("Your name: " +mystudent.getName());
System.out.println("Your CGPA: " +mystudent.findStatus());

} }