小羊爱主
2015-05-08 14:48
请教,为何输入“数据结构”后,打印出来的是乱码?谢谢
package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class SetTest { public List<Course> coursesToSelect; private Scanner console; public Student student; public SetTest(){ this.coursesToSelect=new ArrayList<Course>(); } public void testAdd(){ Course cr1=new Course("1","数据结构"); coursesToSelect.add(cr1); Course cr2=new Course("2","C"); coursesToSelect.add(0, cr2); Course[] course={new Course("3","汇编语言"),new Course("4","计算机原理")}; coursesToSelect.addAll(Arrays.asList(course)); Course[] course2={new Course("5","离散数学"),new Course("6","大学英语")}; coursesToSelect.addAll(4, Arrays.asList(course2)); } public void testForEach(){ System.out.println("待选课程如下:(通过foreach来选择)"); for(Course cr:coursesToSelect){ //Course cr=(Course)obj; System.out.println(cr.id+" "+cr.name); } } public void testListContains(){ Course course=coursesToSelect.get(0); System.out.println("取得课程:"+course.name); System.out.println("备选课程中是否包含课程:"+course.name+","+coursesToSelect.contains(course)); Course course2=new Course(course.id,course.name); System.out.println("新创建课程:"+course2.name); System.out.println("备选课程中是否包含课程:"+course2.name+","+coursesToSelect.contains(course2)); } public void createStudentAndSelectCourse(){ student=new Student("1","小莫"); System.out.println("欢迎学生:"+student.name+"选课!"); console=new Scanner(System.in); for(int i=0;i<3;i++){ System.out.println("请输入课程ID"); String courseId=console.next(); for(Course cr:coursesToSelect){ if(cr.id.equals(courseId)){ student.courses.add(cr); } } } } public void testSetContains(){ System.out.println("请输入学生已选的课程名称:"); console=new Scanner(System.in); String name=console.next(); Course course2=new Course(); course2.name=name; System.out.println("新创建课程:"+course2.name); System.out.println("已选课程中是否包含课程:"+course2.name+","+student.courses.contains(course2)); } public static void main(String[] args) { // TODO Auto-generated method stub SetTest st=new SetTest(); st.testAdd(); st.testForEach(); st.createStudentAndSelectCourse(); st.testForEachForSet(st.student); st.testSetContains(); } public void testForEachForSet(Student student){ System.out.println("共选择了:"+student.courses.size()); for(Course cr:student.courses){ System.out.println("选择了课程:"+cr.name); } } }
这个问题比较奇怪。
楼主可以参考这个:http://bbs.csdn.net/topics/340269600
Java入门第三季
409792 学习 · 4340 问题
相似问题