package demo; import java.util.HashMap; import java.util.Scanner; public class exercise333 { private static Scanner scanner; /** * 学生信息集合 */ private static HashMap<String,Student> map; public static void main(String[] args) { // TODO Auto-generated method stub select(); } private static void select() { System.out.println("请输入学号:"); Scanner sc = new Scanner(System.in); String nub = sc.nextLine(); Student student = map.get(nub); if(student == null) System.out.println("没有查询到该学生!"); else System.out.println(student.toString()); } } class Student{ private String sno; /** * 学号 */ private String name; /** * 姓名 */ private String major; /** * 专业 */ private String score; /** * 成绩 */ public String toString() { return "学生信息{"+ "学号='" + sno + '\'' + "姓名='" + name + '\'' + "专业='" + major + '\'' + "成绩='" + score + '\'' + '}'; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getName() { return name ; } public void setName(String name) { this.name = name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getScore() { return score; } public void setScore(String score) { this.score =score; } public Student(String sno,String name,String major,String score) { this.sno = sno; this.name = name; this.major = major; this.score = score; } }
时间: 2023-05-25 22:02:45 浏览: 164
There are a few issues with the provided code:
1. The `map` object is not initialized, which will cause a `NullPointerException` when using the `get` method. To fix this, initialize the map object before using it:
```
private static HashMap<String,Student> map = new HashMap<String,Student>();
```
2. The `toString` method in the `Student` class is missing some plus signs to concatenate the strings. Fix it by adding the plus signs:
```
public String toString() {
return "学生信息{" +
"学号='" + sno + '\'' +
", 姓名='" + name + '\'' +
", 专业='" + major + '\'' +
", 成绩='" + score + '\'' +
'}';
}
```
3. The `score` field is declared as `String`, which is not a suitable type for storing scores. Change it to a numeric type, such as `int` or `double`.
4. The `setScore` method has a typo and should be `this.score = score;`
5. It's a good practice to use `try-catch` blocks when reading from input, as the input could be incorrect and cause exceptions. For example, using `Integer.parseInt()` to get an integer from input could throw a `NumberFormatException`, and using `scanner.nextLine()` could throw a `NoSuchElementException` if there is no more input available.
Here is the fixed code with these issues addressed:
```
package demo;
import java.util.HashMap;
import java.util.Scanner;
public class Exercise333 {
private static Scanner scanner;
/** 学生信息集合 */
private static HashMap<String, Student> map = new HashMap<String, Student>();
public static void main(String[] args) {
// TODO Auto-generated method stub
select();
}
private static void select() {
System.out.println("请输入学号:");
Scanner sc = new Scanner(System.in);
String nub = sc.nextLine();
try {
Student student = map.get(nub);
if (student == null)
System.out.println("没有查询到该学生!");
else
System.out.println(student.toString());
} catch (Exception e) {
System.out.println("输入有误!");
} finally {
sc.close();
}
}
}
class Student {
private String sno; /** 学号 */
private String name; /** 姓名 */
private String major; /** 专业 */
private int score; /** 成绩 */
public String toString() {
return "学生信息{" +
"学号='" + sno + '\'' +
", 姓名='" + name + '\'' +
", 专业='" + major + '\'' +
", 成绩='" + score + '\'' +
'}';
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String sno, String name, String major, int score) {
this.sno = sno;
this.name = name;
this.major = major;
this.score = score;
}
}
```
阅读全文