下列代碼輸入整型的玩家ID不出錯(cuò),當(dāng)輸入字符,比如a,結(jié)果會(huì)死循環(huán),并且一直重復(fù)顯示,請(qǐng)輸入整數(shù)類型的ID,請(qǐng)輸入玩家1的Id,如何改進(jìn)?public void testBuildPlayer(){ System.out.println("-------創(chuàng)建玩家-------"); while(true)//出錯(cuò)時(shí)候一直循環(huán)?如何解決 { try{ System.out.println("請(qǐng)輸入玩家1的Id"); player1.id=console.nextInt(); System.out.println("請(qǐng)輸入玩家1的姓名"); player1.name=console.next(); System.out.println("請(qǐng)輸入玩家2的Id"); player2.id=console.nextInt(); System.out.println("請(qǐng)輸入玩家2的姓名"); player2.name=console.next(); break; }catch(InputMismatchException e){ System.out.println("請(qǐng)輸入整數(shù)類型的ID"); continue; } } System.out.println("------歡迎玩家:"+player1.name); System.out.println("------歡迎玩家:"+player2.name); }
try,catch使用時(shí)候出了問題,求解釋
葫蘆娃3402926
2016-07-19 10:59:01