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