在循環中使用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