问题遇到的现象和发生背景
学生信息列表功能是怎么实现的?int index =-1意思和功能是什么?为什么要用它?
问题相关代码,请勿粘贴截图
第一段:
package cc;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
while(true){
System.out.println("************************************************");
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("************************************************");
System.out.println("非常感谢您的使用");
System.out.println("请在下面请输入您的选择");
Scanner scanner = new Scanner(System.in);
String x=scanner.nextLine();
switch (x){
case "1":
addStudent(array);
break;
case "2":
deleteStudent(array);
break;
case "3":
changeStudent(array);
break;
case "4":
searchStudent(array);
break;
case "5":
System.out.println("退出");
System.exit(0);
System.out.println("谢谢您的使用,再见");
break;
}
}
}
public static void addStudent(ArrayList<Student> array){
Scanner sc = new Scanner(System.in);
int index =-1;//?//
String sid2="";//?//
//为了保证程序可以回来,用一个循环实现
while (true) {
System.out.println("请输入学生学号");
String sid = sc.nextLine();
for (int i = 0; i < array.size(); i++) //i小于数组的大小//
{
Student s1 = array.get(i);
if (s1.getSid().equals(sid)) {
index = i;
}
}
if(index!=-1){
System.out.println("学生已存在,请重新输入");
index=-1;
}else{
sid2=sid;
break;
}
}
System.out.println("请输入学生姓名");
String name=sc.nextLine();
System.out.println("请输入学生年龄");
String age=sc.nextLine();
System.out.println("请输入学生居住地");
String address=sc.nextLine();
//创建学生对象
Student st = new Student();
st.setSid(sid2);
st.setName(name);
st.setAge(age);
st.setAddress(address);
array.add(st);
System.out.println("添加学生成功");
}
public static void searchStudent(ArrayList<Student> array)//Array是动态数组,因为student的长度是会变的
{
if(array.size()==0){
System.out.println("请先添加学生信息");
return;//程序不再向下执行
}
System.out.println("学号\t\t\t姓名\t\t年龄\t居住地");
for (int i = 0; i < array.size(); i++) {
Student stt = array.get(i);
System.out.println(stt.getSid() + "\t\t" + stt.getName() + "\t\t" + stt.getAge() + "岁" + "\t" + stt.getAddress());
}
}
public static void deleteStudent(ArrayList<Student> array){
System.out.println("请输入你要删除的学生学号");
Scanner scanner = new Scanner(System.in);
String n=scanner.nextLine();
int index=-1;
for (int i = 0; i <array.size() ; i++) {
Student st2 = new Student();
st2=array.get(i);
if(st2.getSid().equals(n)) {
index =i;
array.remove(i);
System.out.println("删除成功");
break;
}
}
if(index==-1){
System.out.println("该学生不存在,请重新输入");
}
}
public static void changeStudent(ArrayList<Student> array){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号");
String c=scanner.nextLine();
int index=-1;
for (int i = 0; i <array.size() ; i++) {
Student st =array.get(i);
if(st.getSid().equals(c)){
index=i;
System.out.println("请输入学生学号");
st.setSid(scanner.nextLine());
System.out.println("请输入学生姓名");
st.setName(scanner.nextLine());
System.out.println("请输入学生年龄");
st.setAge(scanner.nextLine());
System.out.println("请输入学生居住地");
st.setAddress(scanner.nextLine());
System.out.println("修改成功");
}
}
if(index==-1){
System.out.println("该学生不存在");
}
}
}
第二段:
package 实训;
public class Student {
private String sid;
private String Age;
private String Name;
private String Address;
public Student(){}
public Student(String sid,String Name,String Age,String Address){
this.sid=sid;
this.Name=Name;
this.Age=Age;
this.Address=Address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getAge() {
return Age;
}
public void setAge(String Age ) {
this.Age = Age;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getAddress() {
return Address;
}
public void setAddress(String Address) {
this.Address = Address;
}
}