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());
}
}
以下代码没有任何语法错误,但是当我尝试执行它时,用户输入不存储,因此输出变为null。我尝试在TestStudent类中创建运行它,但结果仍然相同。
答案 0 :(得分:0)
下面的代码不会存储您的信息,因为您没有保存它。
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());
答案 1 :(得分:0)
你忘了存储数据:
您需要添加以下行:
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());
}
答案 2 :(得分:0)
此代码中有两个不好的实现。你的findStatus()返回了空(未初始化)字符串,你没有设置(初始化)名称和 cgpa 这是编辑过的代码
公共班学生{
字符串名称;双cgpa;字符串状态;
public void setName(String name){
this.name=name; }
public String getName(){
return name; } public void setCgpa(double cgpa){ this.cgpa=cgpa; }
public double getCgpa(){ 返回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){
学生mystudent =新学生();
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());
}}