Java 從鍵盤輸入不確定的整數 並判斷讀入的整數和負數的個數,輸入0時候結束


 1         /**
 2          * 從鍵盤輸入不確定的整數 並判斷讀入的整數和負數的個數,輸入0時候結束
 3          * 
 4          */
 5           
 6         Scanner scan = new Scanner(System.in);
 7         
 8         int posituveNumber = 0;      //記錄正數的個數
 9         int negativeNumber = 0;      //記錄負數的個數
10         System.out.println("請輸入數字:");
11         
12         while(true)       //for(;;)
13         {
14             int number = scan.nextInt();
15             
16             //判斷number的正負情況
17             if(number >0)
18             {
19                 posituveNumber++;
20             }
21             else if( number < 0)
22             {
23                 negativeNumber++;
24             }
25             else
26             {
27                 //一旦執行break,跳出循環
28                 break;
29             }
30             
31             
32             
33         }
34         
35         System.out.println("輸入的正數個數為:"+posituveNumber);
36         System.out.println("輸入的正數個數為:"+negativeNumber);
37         
38         
39         
40       

 


免責聲明!

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



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