package experimentSecond; import java.util.Scanner; class Student1 { private String name;//姓名 private String id;//学号 private double score;//成绩 public Student1(String name, String id, double result) {//构造方法 this.name = name; this.id = id; this.score = result; } public static void sort(Student1[] stus) {//数组方法 Student1 temp; for (int i = 0; i<stus.length-1;i++) {//冒泡比较 for (int j=0;j<stus.length-i-1;j++) { if (stus[j].score < stus[j+1].score) { temp=stus[j]; stus[j]=stus[j+1]; stus[j+1]=temp; } } } } //输出内容 public void show() { System.out.println("姓名:"+ name +"\t学号:"+id +"\t\t成绩:"+ score); } } public class Student{ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("请输入要录入的学生个数:"); int numStudent=input.nextInt(); Student1[] stus = new Student1[numStudent]; Student1 student = null; //录入学生信息 System.out.println("按要求输入学生信息"); for(int i=0;i<numStudent;i++) { System.out.print("依次输入第"+(i+1)+"个学生的姓名学号成绩:"); stus[i]=new Student1(input.next(),input.next(),input.nextDouble()); } //调用排序方法 student.sort(stus); System.out.println("成绩录入完成,下面将按成绩的降序排序输出学生信息"); System.out.println("------------------------------------------------------"); //按成绩降序输出学生信息 for (Student1 stu : stus) { stu.show(); } } }
时间: 2024-04-05 12:29:43 浏览: 19
这段代码是一个Java程序,用来实现学生信息的录入和排序。程序中定义了一个Student1类,包含姓名、学号和成绩三个属性,以及构造方法和排序方法。在主函数中,通过Scanner类获取用户输入的学生个数和每个学生的姓名、学号和成绩,并将其存储在一个数组中。然后调用Student1类的排序方法将学生信息按成绩降序排列,并输出每个学生的信息。
相关问题
import java.util.Scanner; class Student { private int id; private String name; private int age; private String gender; private double grade; public void setGrade(double grade) { this.grade = grade; } public Stude
这段代码依然不完整,但是我们可以看出这是一个学生类,包含了一些属性和方法。以下是一份完整的学生类代码示例,希望可以帮到您:
```
import java.util.Scanner;
class Student {
private int id;
private String name;
private int age;
private String gender;
private double grade;
public Student() {
this.id = 0;
this.name = "";
this.age = 0;
this.gender = "";
this.grade = 0;
}
public Student(int id, String name, int age, String gender, double grade) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.grade = grade;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public void input() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入学号:");
this.id = sc.nextInt();
System.out.print("请输入姓名:");
this.name = sc.next();
System.out.print("请输入年龄:");
this.age = sc.nextInt();
System.out.print("请输入性别:");
this.gender = sc.next();
System.out.print("请输入成绩:");
this.grade = sc.nextDouble();
}
public void output() {
System.out.println("学号:" + this.id);
System.out.println("姓名:" + this.name);
System.out.println("年龄:" + this.age);
System.out.println("性别:" + this.gender);
System.out.println("成绩:" + this.grade);
}
}
```
这是一个比较基础的学生类,包含了一些常见的属性和方法,例如构造函数、getter和setter方法、输入输出方法等。您可以在此基础上进行扩展和修改。
import java.util.Scanner; package base; public class scaner { public static void main(String[] args) { } } 改错
在代码的开头处移除多余的import语句,将package语句放在import语句后面,同时将类名从"scaner"改为"Scanner",修正后的代码如下:
```java
package base;
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
// 在这里编写你的代码
}
}
```
请注意,你需要在`main`方法中编写你的代码,这只是一个修正后的代码框架。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)