public class TestInput {?? ??? ?/*?? ? * 實(shí)現(xiàn)功能:從控制臺(tái)輸入一個(gè)整數(shù),然后打印出來。?? ? * 當(dāng)輸入字符時(shí)會(huì)提示輸入有誤,接著提示:請(qǐng)輸入一個(gè)整數(shù)?? ? */?? ?public static void main(String[] args) {?? ??? ?Scanner sc = new Scanner(System.in);?? ??? ?int num = 0;?? ??? ?while(true){?? ??? ??? ?System.out.println("請(qǐng)輸入一個(gè)整數(shù):");?? ??? ??? ?try {?? ??? ??? ??? ?num = sc.nextInt();?? ??? ??? ??? ?sc.close();?? ??? ??? ??? ?break;?? ??? ??? ?} catch (Exception e) {?? ??? ??? ??? ?System.out.println("輸入有誤?。?!");?? ??? ??? ?}?? ??? ?}?? ??? ?System.out.println(num);?? ?}}
添加回答
舉報(bào)
0/150
提交
取消