面向对象 对键盘输入的值做判断 不允许学生id重复
类
package com.fqs.demo1; public class Student { private String id; private String name; private int age; //无参数 public Student(){ } //全部参数 public Student(String id,String name,int age){ this.id=id; this.name=name; this.age=age; } //set public void setId(String id) { this.id=id; } //get public String getId() { return id; } //set public void setName(String name) { this.name=name; } //get public String getName() { return name; } //set public void setAge(int age) { this.age=age; } //get public int getAge() { return age; } }
对象
package com.fqs.demo1; import java.util.Scanner; public class StudentTest { public static void main(String[]args) { //1.定义数组arr Student []arr=new Student[3]; //2.给数组赋值 Student s1=new Student("stu001","fqs",36); Student s2=new Student(); //3.放入数组 arr[0]=s1; arr[1]=s2; //4.键盘输入s2 Scanner sc=new Scanner(System.in); //提示输入学生id System.out.println("请输入学生id:"); String id=sc.next(); //判断id和s1中的id 重复吗 while(s1.getId().equals(id)) { //提示输入学生id System.out.println("请再次输入学生id"); id=sc.next(); } while(!s1.getId().equals(id)){ System.out.println("-------"); s2.setId(id); break; } //提示输入学生名字 System.out.println("请输入学生名字"); String name=sc.next(); s2.setName(name); //提示输入学生年龄 System.out.println("请输入学生年龄"); int age=sc.nextInt(); s2.setAge(age); //判断id是否和已存在的重复 //输出s2 System.out.println(s2.getId()+" "+s2.getName()+" "+s2.getAge()); } }