1. 程式人生 > 其它 >Scanner-學習了 else否則 和 while迴圈 求和 求平均數 Demo04 Demo05 Ce4 Ce5

Scanner-學習了 else否則 和 while迴圈 求和 求平均數 Demo04 Demo05 Ce4 Ce5

 1 package com.xl.scanner;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Demo04 {
 6     public static void main(String[] args){
 7         Scanner scanner = new Scanner(System.in);
 8 
 9         //從鍵盤接受資料
10         int i = 0;
11         float f = 0.0f;
12 
13         System.out.println("請輸入整數: ");
14 15 //如果...那麼 16 if(scanner.hasNextInt()){ 17 i = scanner.nextInt();//等待使用者得輸入 18 System.out.println("整數資料: "+ i); 19 }else{//否則 20 System.out.println("輸入的不是整數資料!"); 21 22 } 23 24 System.out.println("請輸入小數: "); 25 26 //
如果...那麼 27 if(scanner.hasNextFloat()){ 28 f = scanner.nextFloat();//改動next Float //等待使用者得輸入 29 System.out.println("小數資料: "+ f); 30 }else{//否則 31 System.out.println("輸入的不是小數資料!"); 32 33 34 35 } 36 37 scanner.close(); 38 39 } 40 }
 1 package
com.xl.scanner; 2 import java.util.Scanner; 3 public class Ce4 { 4 public static void main(String[] args){ 5 Scanner scanner = new Scanner(System.in); 6 int a = 0;//整數 7 float b = 0.0f;//小數 8 long c = 0L;//長整數 9 double d =0.0D;//雙精度浮點型 10 11 System.out.println("請輸入整數: "); 12 13 //整數 14 if(scanner.hasNextInt()){ 15 a = scanner.nextInt();//等待使用者的輸入 16 System.out.println("整數資料: " + a); 17 }else{//否側 18 System.out.println("輸錯了,這不是整數!!!!!"); 19 } 20 21 System.out.println("請輸入小數: "); 22 //小數 23 if (scanner.hasNextFloat()){ 24 b = scanner.nextFloat(); 25 System.out.println("小數資料: "+ b); 26 }else{//否則 27 System.out.println("輸錯了,這不是小數!!!!"); 28 } 29 30 System.out.println("請輸入長整數:"); 31 //長整數 32 if (scanner.hasNextLong()){ 33 c = scanner.nextLong(); 34 System.out.println("長整數資料: " + c); 35 }else{ 36 System.out.println("輸錯了,這不是長整數!!!"); 37 } 38 39 System.out.println("請輸入雙精度浮點數"); 40 //雙精度浮點型 41 if (scanner.hasNextDouble()){ 42 d = scanner.nextDouble(); 43 System.out.println("雙精度浮點數 :" + d); 44 }else{ 45 System.out.println("又輸錯了,這不是雙精度浮點數"); 46 } 47 48 49 50 51 scanner.close(); 52 } 53 }
 1 package com.xl.scanner;
 2 import  java.util.Scanner;
 3 public class Demo05 {
 4     public static void main(String[] args){
 5         //我們可以輸入多個數字,並求其總和與平均數,每輸一個數字用回車確認,通過非數字來結束輸入並輸出執行結果:
 6         Scanner scanner = new Scanner(System.in);
 7 
 8         //
 9         double sum = 0;
10         //計算了輸入了多少個數字
11         int m = 0;
12 
13         //通過while迴圈判斷是否還有輸入,並在裡面對每一次進行求和和統計
14         while(scanner.hasNextDouble()){
15            double x = scanner.nextDouble();//輸入
16            m = m + 1 ;//m++
17            sum = sum + x ;
18            System.out.println("你輸入了第 "+m+"個數據,然後當前結果sum= "+sum);
19         }
20         System.out.println(m + "個數得和為 " + sum);
21         System.out.println(m + "個數的平均值是 " + (sum / m));//總數除以個數就是平均值
22 
23 
24         scanner.close();
25 
26 
27 
28 }
29 }
 1 package com.xl.scanner;
 2 import java.util.Scanner;
 3 public class Ce5 {
 4     public static void main(String[] args){
 5         //我們可以輸入多個數字,並求其總和與平均數,每輸一個數字用回車確認,通過非數字來結束輸入並輸出執行結果:
 6       Scanner scanner = new Scanner(System.in);
 7 
 8       //
 9       double A = 0;//10       //計算了輸入了多少個數字
11       int B = 0;//計算了輸入了多少個數字
12 
13        //通過while迴圈判斷是否還有輸入,並在裡面對每一次進行求和和統計
14        while(scanner.hasNextDouble()){//while迴圈  通過非數字來結束迴圈
15            double C = (scanner.nextDouble());//輸入
16        B = B + 1; //每次輸入後 都+1 或 m++
17        A = A + C;//和等於 已經輸入的數字和 + 現在輸入的數字
18 
19 
20        System.out.println("你輸入了第 " +B+ "個數據,然後當前【和】結果為: "+ A);//顯示輸出了第幾個數 然後這這些輸入的數相加的和為 A
21 
22 
23        }
24        System.out.println(B + "個數的和為:" + A);//B為 輸入了多少個數  這些數相加為 A
25        System.out.println(B + "個數的平均值是:" + (A / B));////B為 輸入了多少個數  再用【和】除以輸入了幾個數 求平均值
26 
27        scanner.close();//結束語句
28 
29 
30 
31 }
32 
33 
34 }