1、编写 Java 程序,输出“序号+学号+姓名”。
时间: 2024-02-03 16:15:14 浏览: 154
以下是 Java 代码实现:
```java
public class StudentInfo {
public static void main(String[] args) {
int number = 1; // 序号
String studentID = "20210001"; // 学号
String name = "张三"; // 姓名
System.out.println(number + "\t" + studentID + "\t" + name);
}
}
```
输出结果为:
```
1 20210001 张三
```
相关问题
1、编写Java程序,输出“序号+学号+姓名”。(
在Java中,你可以通过创建一个简单的类来表示学生信息,并使用对象的多态性来动态地显示不同的属性。假设有一个基础类`Student`,然后有具体的子类如`UndergraduateStudent`和`GraduateStudent`,每个子类都有自己的`序号`、`学号`和`姓名`。
首先,定义基础类`Student`:
```java
public abstract class Student {
protected int sequence;
protected String id;
protected String name;
public Student(int sequence, String id, String name) {
this.sequence = sequence;
this.id = id;
this.name = name;
}
public abstract void displayInfo();
}
```
接着,创建具体的子类并实现`displayInfo()`方法:
```java
public class UndergraduateStudent extends Student {
// 实现displayInfo()方法
@Override
public void displayInfo() {
System.out.println("序号:" + sequence + " 学号:" + id + " 姓名:" + name);
}
}
public class GraduateStudent extends Student {
// 实现displayInfo()方法
@Override
public void displayInfo() {
System.out.println("序号:" + sequence + " 学号:" + id + " 姓名:" + name + " (研究生)");
}
}
```
最后,在主函数中实例化并展示信息:
```java
public static void main(String[] args) {
Student student1 = new UndergraduateStudent(1, "001", "张三");
Student student2 = new GraduateStudent(2, "002", "李四");
student1.displayInfo(); // 输出大学生的信息
student2.displayInfo(); // 输出研究生的信息
}
```
编写三个系别的学生类:英语系,计算机系,文学系(要求继承学生类)各系有以下成绩:(知识点:面向对象综合应用、数组中元素为引用类型) 英语系:演讲,期末考试,期中考试; 计算机系:操作能力,英语写作,期中考试,期末考试; 文学系: 演讲,作品,期末考试,期中考试;各系总分评测标准: 英语系: 演讲 50% 期末考试 25% 期中考试 25% 计算机系: 操作能力 40% 英语写作 20% 期末考试 20% 期中考试 20% 文学系: 演讲 35% 作品 35% 期末考试 15% 期中考试 15% 定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息: 序号:1,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:2,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:3,学号:XXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:4,学号:XXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:5,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX
下面是一个示例代码实现:
```java
import java.util.Random;
class Student {
private String id; // 学号
private String name; // 姓名
private char gender; // 性别
private int age; // 年龄
private double score; // 成绩
public Student(String id, String name, char gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
class EnglishStudent extends Student {
private double speech; // 演讲成绩
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public EnglishStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.speech = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getSpeech() {
return speech;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.5 * speech + 0.25 * finalExam + 0.25 * midtermExam;
}
}
class ComputerStudent extends Student {
private double ability; // 操作能力
private double writing; // 英语写作
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public ComputerStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.ability = rand.nextDouble() * 100;
this.writing = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getAbility() {
return ability;
}
public double getWriting() {
return writing;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.4 * ability + 0.2 * writing + 0.2 * finalExam + 0.2 * midtermExam;
}
}
class LiteratureStudent extends Student {
private double speech; // 演讲成绩
private double essay; // 作品成绩
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public LiteratureStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.speech = rand.nextDouble() * 100;
this.essay = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getSpeech() {
return speech;
}
public double getEssay() {
return essay;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.35 * speech + 0.35 * essay + 0.15 * finalExam + 0.15 * midtermExam;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new EnglishStudent("20210001", "张三", 'M', 20);
students[1] = new EnglishStudent("20210002", "李四", 'F', 19);
students[2] = new ComputerStudent("20210003", "王五", 'M', 21);
students[3] = new ComputerStudent("20210004", "赵六", 'F', 20);
students[4] = new LiteratureStudent("20210005", "钱七", 'M', 22);
// 计算综合成绩
for (int i = 0; i < students.length; i++) {
students[i].setScore(0);
}
// 排序
for (int i = 0; i < students.length - 1; i++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].getScore() < students[j].getScore()) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
// 输出结果
for (int i = 0; i < students.length; i++) {
System.out.println("序号:" + (i + 1) + ",学号:" + students[i].getId() + ",姓名:" + students[i].getName()
+ ",性别:" + students[i].getGender() + ",年龄:" + students[i].getAge() + ",综合成绩:"
+ students[i].getScore());
}
}
}
```
这个代码中,我们定义了一个`Student`类作为基类,然后分别定义了`EnglishStudent`、`ComputerStudent`和`LiteratureStudent`类继承自`Student`类,并分别添加了各自的成绩属性和计算综合成绩的方法。在`Main`类中,我们创建了一个长度为5的`Student`数组,使用随机数给该数组装入各系学生的对象,并计算出各个学生的综合成绩,最后按照综合成绩从高到低排序并输出结果。
阅读全文