在这些代码中运行SetTest的主函数总是输入时多了个尾巴
如以下运行结果:
现有如下课程待选:1>大学英语 2>高等数学 3>大学语文 4>大学物理 5>离散数学
Welcomenullto pick class
请输入预选课程ID:1
请输入预选课程ID:2
你已经选择如下课程大学英语 高等数学 请输入查询的课程:大学英语
你输入的是:大学英语
请输入查询的课程:高等数学
你输入的是:高等数学
备选课程是否包括[Course [id=null, name=大学英语], Course [id=null, name=高等数学]]:true
检查已选课程
请输入已选课程|
大学英语
已选课程是否有dx大学英语:false
是否继续呀?(是/不)
你看,最后本来输入的是大学英语,可却成了dx大学英语。已经确定不是键盘的问题了,这是什么情况呢[AppleScript] 纯文本查看 复制代码 package com.imooc.colection;
/**
* 课程类
* @author Administrator
*
*/
public class Course {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//创建课程类含参构造器
public Course(String id,String name){
this.id=id;
this.name=name;
}
/**
*
*/
public Course(){
}
public String toString() {
return "Course [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Course))
return false;
Course other = (Course) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.imooc.colection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest {
//创建一个list容器用于存放备选课程
private List coursesToSelect;
public List getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
//创建构造器
public ListTest(){
this.coursesToSelect=new ArrayList();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("1","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
}
}
package com.imooc.colection;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
private String id;
private String name;
private Set<Course> courses;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Course courses) {
this.courses.add(courses);
}
//创建含参构造器
public Student(String id,String namen){
this.id=id;
this.name=name;
this.courses=new HashSet<Course>();
}
}
package com.imooc.colection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
/**
* 创建一容器coursesToSelect,用于盛放待选课程
*/
private List<Course>coursesToSelect;
public List<Course> getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List<Course> coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
/**
* 添加一构造器用于初始化coursesToSelect
*/
public SetTest(){
this.coursesToSelect=new ArrayList<Course>();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("2","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
System.out.print("现有如下课程待选:");
for(int i=0;i<cr.length;i++){
System.out.print(cr[i].getId()+">"+cr[i].getName()+" ");
}
System.out.println();
}
/**
* 创建选课方法
*/
public void testPick(Student student,SetTest it){
System.out.println("Welcome"+student.getName()+"to pick class");
Scanner input =new Scanner(System.in);
for(int i=0;i<2;i++){
i:
do{
System.out.print("请输入预选课程ID:");
String id=input.next();
for(Course cr:coursesToSelect){
if(cr.getId().equals(id)){
student.setCourses(cr);
break i;
}
}
System.out.println("输入错误");
}while(true);
}
System.out.print("你已经选择如下课程");
for(Course cr :student.getCourses()){
System.out.print(cr.getName()+" ");
}
}
public void testListContains(){
Scanner input=new Scanner(System.in);
Course[] cr2=new Course[2];
for(int i=0;i<2;i++){
cr2[i]=new Course();
System.out.print("请输入查询的课程:");
String in=input.next();
System.out.println("你输入的是:"+in);
cr2[i].setName(in);
}
System.out.println("备选课程是否包括"+Arrays.toString(cr2)+":"+coursesToSelect.containsAll(Arrays.asList(cr2)));
}
public void testASetContens(Student stu){
System.out.println("检查已选课程");
Scanner input=new Scanner(System.in);
boolean check=false;
do{
System.out.println("请输入已选课程|");
Course cr=new Course(null,input.next());
boolean s=stu.getCourses().contains(cr);
System.out.println("已选课程是否有"+cr.getName()+":"+s);
System.out.println("是否继续呀?(是/不)");
if(input.next().equals("是"))
check=true;
else
check=false;
}while(check);
}
public static void main(String[] args) {
SetTest it=new SetTest();
it.testAdd();
Student stu=new Student("1","jack");
it.testPick(stu,it);
it.testListContains();
it.testASetContens(stu);
}
}
|