Student.java:18: 错误: 不兼容的类型 this.num = num; ^ 需要: int 找到: String 1 个错误
时间: 2024-10-25 10:12:44 浏览: 9
这个错误提示是因为你在试图将一个字符串类型的`String num`赋值给一个期望整数类型的变量`int this.num`。在Java中,这是不允许的类型转换。你需要确保在构造方法中,传递给`num`字段的是一个`int`类型的值。
如果你确实想从字符串转换为整数,你应该使用`Integer.parseInt()`或者`Character.getNumericValue()`方法来进行转换,例如:
```java
// 修改后的构造方法 - 需要学号和姓名
public Student(String numStr, String name) {
this(); // 调用无参数构造方法,确保先设置默认信息
try {
this.num = Integer.parseInt(numStr); // 将字符串转换为整数
} catch (NumberFormatException e) {
System.out.println("输入的学号不是有效的数字!");
this.num = 0; // 如果转换失败,可以设置默认值或者处理异常
}
this.name = name;
}
```
现在,如果`numStr`不是一个有效的数字,程序会捕获`NumberFormatException`并给出相应的错误消息。
相关问题
#include<iostream> using namespace std; class student { private: string name; int age; const int id; static int num; public: // 构造函数 student(); student(string, int age = 20, int id = 20201120); student(const student& s); // 成员函数 void set_name(string name); void set_age(int age); string get_name() const; int get_age() const; int get_id() const; static int get_num(); void print(void) { cout << name<< " " << age << " " << id << endl; } // 友元函数 friend bool operator==(const student& s1, const student& s2); // 析构函数 ~student(); }; int student::num = 0; // 构造 student::student() : name("null"), age(0), id(20201120+num) {} student::student(string name, int age, int id) : name(name), age(age), id(id) { ++num; } student::student(const student& s) : name(s.name), age(s.age), id(20201120+num) {} // 成员 void student::set_name(std::string name) { this->name = name; } void student::set_age(int age) { this->age = age; } string student::get_name() const { return name; } int student::get_age() const { return age; } int student::get_id() const { return id; } int student::get_num() { return num; } // 友元 bool operator==(const student& s1, const student& s2) { return (s1.name == s2.name) && (s1.age == s2.age); } // 析构 student::~student() { num--; } //对比 int equals(student s1, student s2) { if (s1 == s2) { cout << "True" << endl;} else { cout << "False" << endl;} return 0; } int main() { student s1; s1.print(); //带参数有默认值 student s2("小红",18); s2.print(); //使用set更改姓名 s2.set_name("小蓝"); s2.print(); student s3(s2); s3.print(); //测试友元函数是否相同 equals(s2, s3); }对这段代码的输出进行分析
这段代码的输出如下:
```
null 0 20201120
小红 18 20201121
小蓝 18 20201121
小蓝 18 20201122
True
```
代码中创建了三个student对象,s1使用默认构造函数创建,输出为"null 0 20201120";s2使用带参数的构造函数创建,输出为"小红 18 20201121";s2调用set_name函数将姓名更改为"小蓝",再次输出为"小蓝 18 20201121";s3使用拷贝构造函数从s2创建,输出为"小蓝 18 20201122";最后调用equals函数比较s2和s3,因为两者姓名和年龄相同,所以输出为True。
Java学生选课系统实现: 1. 系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。 2.基本功能需求: (1)使用命令行完成系统的功能设计; (2)启动程序,输出“学生选课管理——登录”。 (2)提示客户输入学号、密码。 3.其他功能需求: (1) 文件CourseDB.java:单例类,模拟数据库中的课程,初始化后至少封装10门课程; (2)文件StudentDB.java:单列类,模拟数据库中的课程,初始化后至少封装5名学生信息; (3)文件Login.java:负责登录的业务逻辑; (4)文件SelectCourse.java:负责选课的业务逻辑; (5)文件MainProcess.java:负责整体业务流程; (6)学生选课成功后,选课状态变更为“已选”,否则,课程状态变更为“可选”;学生选择“已选”课程,提示“该课程无法重复选择”; (7)学生选课成功后,选课人数相应发生变化;代码
以下是一个基本的Java学生选课系统的代码实现,包含了上述所述的所有信息和功能需求:
Course.java
```java
public class Course {
private int id;
private String name;
private int selectedNum;
private int maxNum;
private String teacher;
public Course(int id, String name, int maxNum, String teacher) {
this.id = id;
this.name = name;
this.maxNum = maxNum;
this.teacher = teacher;
this.selectedNum = 0;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getSelectedNum() {
return selectedNum;
}
public int getMaxNum() {
return maxNum;
}
public String getTeacher() {
return teacher;
}
public boolean isSelected() {
return selectedNum >= maxNum;
}
public void select() {
selectedNum++;
}
public void unselect() {
selectedNum--;
}
}
```
CourseDB.java
```java
import java.util.ArrayList;
import java.util.List;
public class CourseDB {
private static CourseDB instance = null;
private List<Course> courses;
private CourseDB() {
courses = new ArrayList<Course>();
courses.add(new Course(1, "Java Programming", 30, "Tom"));
courses.add(new Course(2, "Data Structures", 30, "Jerry"));
courses.add(new Course(3, "Algorithm Design", 30, "Lucy"));
courses.add(new Course(4, "Database Systems", 30, "Lily"));
courses.add(new Course(5, "Operating Systems", 30, "Bob"));
courses.add(new Course(6, "Computer Networks", 30, "Alice"));
courses.add(new Course(7, "Software Engineering", 30, "John"));
courses.add(new Course(8, "Web Development", 30, "Mary"));
courses.add(new Course(9, "Artificial Intelligence", 30, "David"));
courses.add(new Course(10, "Mobile App Development", 30, "Amy"));
}
public static CourseDB getInstance() {
if (instance == null) {
instance = new CourseDB();
}
return instance;
}
public List<Course> getCourses() {
return courses;
}
public Course getCourseById(int id) {
for (Course course : courses) {
if (course.getId() == id) {
return course;
}
}
return null;
}
}
```
Student.java
```java
public class Student {
private int id;
private String name;
private String gender;
private String grade;
private String password;
public Student(int id, String name, String gender, String grade, String password) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getGrade() {
return grade;
}
public String getPassword() {
return password;
}
}
```
StudentDB.java
```java
import java.util.ArrayList;
import java.util.List;
public class StudentDB {
private static StudentDB instance = null;
private List<Student> students;
private StudentDB() {
students = new ArrayList<Student>();
students.add(new Student(1, "Tom", "Male", "Sophomore", "123456"));
students.add(new Student(2, "Jerry", "Male", "Freshman", "123456"));
students.add(new Student(3, "Lucy", "Female", "Senior", "123456"));
students.add(new Student(4, "Lily", "Female", "Junior", "123456"));
students.add(new Student(5, "Bob", "Male", "Senior", "123456"));
}
public static StudentDB getInstance() {
if (instance == null) {
instance = new StudentDB();
}
return instance;
}
public List<Student> getStudents() {
return students;
}
public Student getStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
}
```
Login.java
```java
import java.util.Scanner;
public class Login {
public static Student login() {
Scanner scanner = new Scanner(System.in);
System.out.println("学生选课管理——登录");
System.out.print("请输入学号:");
int id = scanner.nextInt();
System.out.print("请输入密码:");
String password = scanner.next();
StudentDB studentDB = StudentDB.getInstance();
Student student = studentDB.getStudentById(id);
if (student == null || !student.getPassword().equals(password)) {
System.out.println("学号或密码错误,请重新输入!");
return null;
}
return student;
}
}
```
SelectCourse.java
```java
import java.util.List;
import java.util.Scanner;
public class SelectCourse {
private static void printCourses(List<Course> courses) {
System.out.println("所有课程列表:");
System.out.println("编号\t名称\t已选人数\t可选人数\t授课教师");
for (Course course : courses) {
System.out.printf("%d\t%s\t%d\t
阅读全文