我有这个程序,必须询问用户是只想输入学生姓名还是输入学生姓名和横幅 ID。然后他们可以输入“just name”或“both”,然后会出现相应的问题。使用Student我创建的类,我必须使用适当的构造函数将答案打印到屏幕上,或者只是学生姓名或学生姓名和横幅 ID。我想我对如何创建构造函数感到困惑,指令说要创建三个构造函数,一个接受名称和横幅 ID,一个只接受名称,一个不接受任何参数,它希望我创建他们在Student班级中,我认为他们是在main班级内部创建的以访问Student班级。
package classwork6_2;
import java.util.Scanner;
public class ClassWork6_2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Would you like to enter student's name only or name and banner ID?: ");
String response = s.nextLine();
String name;
long banID;
if(response.equalsIgnoreCase("just name")){
System.out.print("Enter student's name: ");
name = s.nextLine();
} else if(response.equalsIgnoreCase("both")){
System.out.print("Enter students name: ");
name = s.nextLine();
System.out.print("Enter student's banner ID: ");
banID = s.nextLong();
}
Student nameBanID = new Student();
nameBanID.setNameBanID(name, banID);
Student n = new Student();
n.setName(name);
System.out.print("Students name is: " + n.getName());
System.out.print("Student's banner ID is: " + n.getNameBanID());
}
}
学生班
package classwork6_2;
public class Student {
private String name;
private String bannerID;
Student nameBanID = new Student();
Student n = new Student();
Student none = new Student();
public String getNameBanID(){
return bannerID + name;
}
public String getName(){
return name;
}
public void setNameBanID(String name, long banID){
bannerID = bannerID + name;
}
public void setName(String name){
this.name = name;
}
}
qq_遁去的一_1
繁星coding
米脂
随时随地看视频慕课网APP
相关分类