System.out.println("请输入第"+(i+1)+"个人的学号:"); int num=scanner.nextInt(); System.out.println("请输入第"+(i+1)+"个人的姓名:"); String names=scanner.nextLine(); scanner.nextLine(); System.out.println("请输入第"+(i+1)+"个人的所学科目:"); String sub=scanner.nextLine(); Student student=new Student(num,names,sub); studentList.add(student);这个代码为什么无法输出姓名
时间: 2024-01-25 08:02:41 浏览: 60
这个问题可能是由于 `scanner.nextLine()` 在读取完输入数字后没有消耗掉输入缓冲区中的回车符导致的。因此,当你在输入姓名时,`scanner.nextLine()` 会读取到输入缓冲区中的回车符,从而直接跳过了输入姓名的步骤,因此无法输出姓名。
解决方法是在读取完数字后,使用 `scanner.nextLine()` 来将回车符消耗掉,然后再读取姓名,如下所示:
```
System.out.println("请输入第" + (i+1) + "个人的学号:");
int num = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入第" + (i+1) + "个人的姓名:");
String names = scanner.nextLine();
System.out.println("请输入第" + (i+1) + "个人的所学科目:");
String sub = scanner.nextLine();
Student student = new Student(num, names, sub);
studentList.add(student);
```
这样就可以正常输出姓名了。
相关问题
Scanner scanner1=new Scanner(System.in); String a=scanner1.nextLine(); switch (a) { case "学生": { System.out.println("请输入想要存储的学生人数:"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); List<Student> studentList = new ArrayList<>(); for (int i=0;i<number;i++) { System.out.println("请输入第"+(i+1)+"个人的学号:"); int num = scanner.nextInt(); scanner.nextLine(); System.out.println("请输入第"+(i+1)+"个人的姓名:"); String names = scanner.nextLine(); System.out.println("请输入第"+(i+1)+"个人的所学科目:"); String sub = scanner.nextLine(); Student student = new Student(num, names, sub); studentList.add(student); } for (int i=0;i<number;i++) { System.out.println("第"+(i+1)+"个学生的信息"); System.out.println("学号:"+studentList.get(i).getNumber()); System.out.println("姓名:"+studentList.get(i).getName()); System.out.println("科目:"+studentList.get(i).getSubject()); System.out.println(); } }这串代码如何使用hashmap索引list里面的科目
可以将List<Student>中的每个Student对象的subject作为key,将对应的Student对象作为value存入HashMap<String, Student>中,然后就可以通过key来获取对应的Student对象了。代码如下:
```
Map<String, Student> map = new HashMap<>();
for (Student student : studentList) {
map.put(student.getSubject(), student);
}
```
然后可以通过如下方式来获取对应科目的学生信息:
```
String sub = "数学"; // 假设要获取数学科目的学生信息
Student student = map.get(sub);
if (student != null) {
System.out.println("学号:" + student.getNumber());
System.out.println("姓名:" + student.getName());
System.out.println("科目:" + student.getSubject());
}
```
public class Student { private String number;//学号 private String name;//姓名 private String sex;//性别 private String class1;//班级 public Student(String number, String name, String sex, String class1) {//有参构造方法 this.number = number; this.name = name; this.sex = sex; this.class1 = class1; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getClass1() { return class1; } public void setClass1(String class1) { this.class1 = class1; } public class void main add (String[] args) { Scanner in = new Scanner(System.in); Student[] stu = new Student[100]; int num = 0; while (true) { System.out.println("请输入要执行的操作:"); System.out.println("1.添加学生信息"); System.out.println("2.查看学生信息"); System.out.println("3.退出程序"); int choice = in.nextInt(); if (choice == 1) { if (num > stu.length - 1) { System.out.println("系统成员已满"); continue; } System.out.println("请输入学号"); String number = in.next(); for (int i = 0; i < num; i++) { if (stu[i].getNumber().equals(number)) { System.out.println("学号输入重复,请重新输入"); number = in.next(); i = -1; } } System.out.println("请输入姓名"); String name = in.next(); System.out.println("请输入性别"); String sex = in.next(); System.out.println("请输入班级"); String class1 = in.next(); Student student = new Student(number, name, sex, class1); stu[num] = student; num++; System.out.println("添加成功"); } else if (choice == 2) { if (num == 0) { System.out.println("没有学生信息"); continue; } System.out.println("学号\t姓名\t性别\t班级"); for (int i = 0; i < num; i++) { Student student = stu[i]; System.out.println(student.getNumber() + "\t" + student.getName() + "\t" + student.getSex() + "\t" + student.getClass1()); } } else if (choice == 3) { System.out.println("程序已退出"); break; } else { System.out.println("请输入正确的操作编号"); } } } } }中public class void main add (String[] args) {在jdk进行编译时标识符错误
在Java中,一个类只能有一个main方法,而且其签名必须是:
```
public static void main(String[] args)
```
您的代码中,出现了一个名为add的非法方法签名:
```
public class void main add (String[] args)
```
因此,在编译时出现了标识符错误。正确的写法应该是:
```
public static void main(String[] args)
```
请将您的代码改为以下形式:
```
public class Student {
// 省略其他代码
public static void main(String[] args) {
// 程序逻辑
}
}
```
阅读全文