在循环中使用try—catch语句块时,其中含用nextInt()之类的方法,假如输入类型不对,则会抛出异常,并且一直循环:
原因如下:
当某一扫描器抛出 InputMismatchException 时,该扫描器不会传递导致该异常的标记,因此可以通过其他某种方法来获取或跳过它。
这是java API文档中的原话。意思是说,如果nextInt方法没有成功解析出一个整数,那么你的输入不会被忽略。因为它或许能被其它格式解析。
原因如下:
当某一扫描器抛出 InputMismatchException 时,该扫描器不会传递导致该异常的标记,因此可以通过其他某种方法来获取或跳过它。
这是java API文档中的原话。意思是说,如果nextInt方法没有成功解析出一个整数,那么你的输入不会被忽略。因为它或许能被其它格式解析。
2015-01-29
那么你的程序中输入了a后,出现异常,number被赋值100,开始下一次循环,但这时数据缓冲区内你上次输入的a还在,并没有清除,这时继续解析a,还是错误,于是这个过程就一直重复下去了。
现在你要改的是,出现错误把这次的输入清除,只要在catch中,加一句:input.next()就搞定了!
也可以将Scanner写在try里面
现在你要改的是,出现错误把这次的输入清除,只要在catch中,加一句:input.next()就搞定了!
也可以将Scanner写在try里面
2015-01-29
在循环中使用try—catch语句块时,其中含用nextInt()之类的方法,假如输入类型不对,则会抛出异常,并且一直循环:
原因如下:
当某一扫描器抛出 InputMismatchException 时,该扫描器不会传递导致该异常的标记,因此可以通过其他某种方法来获取或跳过它。
这是java API文档中的原话。意思是说,如果nextInt方法没有成功解析出一个整数,那么你的输入不会被忽略。因为它或许能被其它格式解析。
原因如下:
当某一扫描器抛出 InputMismatchException 时,该扫描器不会传递导致该异常的标记,因此可以通过其他某种方法来获取或跳过它。
这是java API文档中的原话。意思是说,如果nextInt方法没有成功解析出一个整数,那么你的输入不会被忽略。因为它或许能被其它格式解析。
2015-01-29
for(Course cr:student.courses){
public void testForEachForSet(Student){
public void testForEachForSet(Student){
2015-01-28
system.out.println();
String courseId=console.next();
if(cr.id.equals(courseId)){
student
String courseId=console.next();
if(cr.id.equals(courseId)){
student
2015-01-28
hashSet;
SET集;
public List<course>coursesToSelect;
public SetText(){
coursesToselect
SetTest st=new setTEXT();
st.testadd();
st.testForEach();
Studnet srudent=new Studnet();
system.out.println();
Scanner console=new Scanner(system.in);
for(int i=0;i<3;i++){
SET集;
public List<course>coursesToSelect;
public SetText(){
coursesToselect
SetTest st=new setTEXT();
st.testadd();
st.testForEach();
Studnet srudent=new Studnet();
system.out.println();
Scanner console=new Scanner(system.in);
for(int i=0;i<3;i++){
2015-01-28
public void testBasicType(){
list<int>list=new arraylist<integer>();
list.add(1);
system.out.println(list.get());
list<int>list=new arraylist<integer>();
list.add(1);
system.out.println(list.get());
2015-01-28
fanxingjihezhongbunengtianjiafangzingyiweide de duixiang;
Course cr2=new Course()j;
Course cr2=new Course()j;
2015-01-28
泛型:集合中的元素,
public list<course>courses;
public TestGeneric(){
this.courses=new ArrayList
public void testAdd(
){
course crl=new Course();
courses.add();
courses.add();
public list<course>courses;
public TestGeneric(){
this.courses=new ArrayList
public void testAdd(
){
course crl=new Course();
courses.add();
courses.add();
2015-01-28
public void testType(){
coursesToSelect.add();
system.out.println();
coursesToSelect.add();
system.out.println();
2015-01-28