Java中创建一个类与对象的实训
实训要就:创建一个StudentData2的程序,可以输入学生姓名和成绩,显示该学生是否通过该门课程。这里将用到两个类:1.Student类。包括一个构造方法和一个被称作...
实训要就:创建一个StudentData2的程序,可以输入学生姓名和成绩,显示该学生是否通过该门课程。
这里将用到两个类:
1.Student类。包括一个构造方法和一个被称作GetResult的方法;通过构造方法来初始化Student类的一些属性;GetResult方法用来判断Student类的对象是否通过所修课程;
2.StudentData2类。包括一个main方法。在该方法中创建2个Student类型的对象,并分别调用GetResult()方法;
3.Student类包括学生姓名和成绩两个私有变量(属性);
4.Student类的构造方法用来对Student类的两个属性设置值;
5.在Student类中包含了GetResult()方法,该方法用来计算Student类的对象所修课及格与否并显示相关信息。
输出结果范例:
=========================
Enter Name: Mike
Enter Mark: 80
Enter Name: Tom
Enter Mark: 30
Mike has passed!
Tom has failed!
========================
大致框架如图 展开
这里将用到两个类:
1.Student类。包括一个构造方法和一个被称作GetResult的方法;通过构造方法来初始化Student类的一些属性;GetResult方法用来判断Student类的对象是否通过所修课程;
2.StudentData2类。包括一个main方法。在该方法中创建2个Student类型的对象,并分别调用GetResult()方法;
3.Student类包括学生姓名和成绩两个私有变量(属性);
4.Student类的构造方法用来对Student类的两个属性设置值;
5.在Student类中包含了GetResult()方法,该方法用来计算Student类的对象所修课及格与否并显示相关信息。
输出结果范例:
=========================
Enter Name: Mike
Enter Mark: 80
Enter Name: Tom
Enter Mark: 30
Mike has passed!
Tom has failed!
========================
大致框架如图 展开
2个回答
展开全部
import java.util.*;
public class StudentData2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name:");
String name1 = sc.next();
System.out.print("Enter Mark:");
int mark1 = sc.nextInt();
System.out.print("Enter Name:");
String name2 = sc.next();
System.out.print("Enter Mark:");
int mark2 = sc.nextInt();
Student stu1 = new Student(name1, mark1);
stu1.getResult();
Student stu2 = new Student(name2, mark2);
stu2.getResult();
}
}
class Student {
private String name;
private int mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public Student(String name, int mark) {
super();
this.name = name;
this.mark = mark;
}
public void getResult() {
if (this.getMark()> 60) {
System.out.println(this.getName() + " has passed!");
} else {
System.out.println(this.getName() + " has failed!");
}
}
}
public class StudentData2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name:");
String name1 = sc.next();
System.out.print("Enter Mark:");
int mark1 = sc.nextInt();
System.out.print("Enter Name:");
String name2 = sc.next();
System.out.print("Enter Mark:");
int mark2 = sc.nextInt();
Student stu1 = new Student(name1, mark1);
stu1.getResult();
Student stu2 = new Student(name2, mark2);
stu2.getResult();
}
}
class Student {
private String name;
private int mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public Student(String name, int mark) {
super();
this.name = name;
this.mark = mark;
}
public void getResult() {
if (this.getMark()> 60) {
System.out.println(this.getName() + " has passed!");
} else {
System.out.println(this.getName() + " has failed!");
}
}
}
追问
可以用
import java.io.*
来做吗?就是其中有抛出IO异常并且有BufferedReader in=new BufferedReader(new InputStreamReader(Sysem.in))那种的,我是初学者,麻烦了
追答
import java.io.*;
public class StudentData2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Name:");
String name1 = br.readLine();
System.out.print("Enter Mark:");
int mark1 = Integer.parseInt(br.readLine());
System.out.print("Enter Name:");
String name2 = br.readLine();
System.out.print("Enter Mark:");
int mark2 = Integer.parseInt(br.readLine());
Student stu1 = new Student(name1, mark1);
stu1.getResult();
Student stu2 = new Student(name2, mark2);
stu2.getResult();
}
}
class Student {
private String name;
private int mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public Student(String name, int mark) {
super();
this.name = name;
this.mark = mark;
}
public void getResult() {
if (this.getMark()>= 60) {
System.out.println(this.getName() + " has passed!");
} else {
System.out.println(this.getName() + " has failed!");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询