Java-判斷輸入數字的奇偶性


 
         
 1 import java.io.InputStream;  2 import java.util.Scanner;  3 
 4 public class ParityCheck {  5     public static void main(String[] args) {  6         boolean boo=true;  7         while(boo){  8             System.out.println("enter a number");  9             try {//排除用戶輸入字符
10                 Scanner input=new Scanner(System.in);//接受用戶輸入的字符
11                 int number=input.nextInt(); 12                 if(number%2==0){//判斷奇偶性
13                     System.out.println("it is an even number"); 14  } 15                 else
16  { 17                     System.out.println("it is odd"); 18  } 19             } catch (Exception e) { 20                 System.out.println("please enter a correct number instead of a character"); 21                 continue; 22  } 23             boo=false; 24  } 25  } 26 }
 
         

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM