分析以下代码import java.util.Scanner; public class IsGrade { private String name; private int grade; public boolean setGrade(int grade) { if (grade >= 60 && grade <= 100) { this.grade = grade; return true; } else { return false; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("input name: "); String name = scanner.nextLine(); System.out.print("input grade: "); int grade = scanner.nextInt(); IsGrade isGrade = new IsGrade(); boolean isGradeValid = isGrade.setGrade(grade); if (isGradeValid) { System.out.println("name: " + name); System.out.println("grade: " + isGrade.grade); System.out.println("success!"); } else { System.out.println("failed!"); } } }
时间: 2024-04-02 09:36:30 浏览: 90
这段代码定义了一个名为IsGrade的类,其中有两个私有字段name和grade。其中,setGrade方法用于设置成绩,只有当传入的成绩grade在60到100之间时,才会将grade字段设置为传入的值,否则返回false。main方法中通过Scanner类获取用户输入的姓名和成绩,然后创建了一个IsGrade对象,并调用其setGrade方法来设置成绩。如果设置成功,就输出姓名、成绩和"success!",否则输出"failed!"。可以看出,这段代码的作用是判断输入的成绩是否合法,并输出相应的信息。
相关问题
5、(类的私有成员与公共成员)编写一个Java程序,要求在程序中定义一个 IsGrade类,其中声明了字符串型变量 name 和私有成员变量 grade,定义方法 setGrade()判断 grade 的值是否为60 和100之间,若是则用参数grade给成员变量grade赋值,并返回true,否则返回false。在主方法中输入姓名和成绩,然后调用setGrade()进行判断并输出相应的结果;
好的,下面是 Java 代码实现:
```java
import java.util.Scanner;
public class IsGrade {
private int grade;
private String name;
public boolean setGrade(int grade) {
if (grade >= 60 && grade <= 100) {
this.grade = grade;
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
IsGrade student = new IsGrade();
System.out.print("请输入姓名:");
student.name = scanner.next();
System.out.print("请输入成绩:");
int grade = scanner.nextInt();
if (student.setGrade(grade)) {
System.out.println(student.name + "的成绩是:" + student.grade);
} else {
System.out.println("输入的成绩不符合要求!");
}
}
}
```
运行程序后,输入姓名和成绩,程序会判断成绩是否在60和100之间,如果符合要求则将成绩赋值给私有成员变量grade并输出,否则提示输入的成绩不符合要求。
补充Student类和StudentManage类,实现学生信息录入、排序、按学号查询学生(如果查询的学生不存在,输出提示信息), 其中,Student类的成员变量包括:String id ; //学号 String name ; //姓名int grade; //成绩,通过学号删除学生,如果要删除的学生不存在,输出提示信息,修改学生成绩功能,添加一个菜单项 “修改成绩”。请用户输入学号、新成绩,修改指定学生的成绩。如果要修改的学生不存在
好的,下面是代码实现:
Student类:
```java
public class Student implements Comparable<Student> {
private String id; //学号
private String name; //姓名
private int grade; //成绩
public Student(String id, String name, int grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + grade;
}
}
```
StudentManage类:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentManage {
private ArrayList<Student> studentList;
public StudentManage() {
studentList = new ArrayList<>();
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生成绩:");
int grade = scanner.nextInt();
Student student = new Student(id, name, grade);
studentList.add(student);
System.out.println("添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
studentList.remove(i);
System.out.println("删除成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("要删除的学生不存在!");
}
}
public void modifyGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改成绩的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
System.out.println("请输入新的成绩:");
int grade = scanner.nextInt();
student.setGrade(grade);
System.out.println("修改成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("要修改成绩的学生不存在!");
}
}
public void queryStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询成绩的学生学号:");
String id = scanner.nextLine();
boolean flag = false;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
System.out.println(student.toString());
flag = true;
break;
}
}
if (!flag) {
System.out.println("要查询成绩的学生不存在!");
}
}
public void sortStudent() {
Collections.sort(studentList);
System.out.println("按学号排序成功!");
for (Student student : studentList) {
System.out.println(student.toString());
}
}
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改成绩");
System.out.println("4.按学号查询学生");
System.out.println("5.按学号排序");
System.out.println("0.退出程序");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取多余的换行符
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyGrade();
break;
case 4:
queryStudent();
break;
case 5:
sortStudent();
break;
case 0:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
public static void main(String[] args) {
StudentManage studentManage = new StudentManage();
studentManage.menu();
}
}
```
在`main`方法中创建一个`StudentManage`对象,调用`menu`方法即可开始使用学生管理系统。
阅读全文