標籤:成功 new default 三目運算 close 編程練習 sum 產生 隨機
一:讓電腦隨機產生100個0到100之間的整數,每個整數代表某個學生的成績,計算這些成績的平均值
public static void main(String[] args) { int sum = 0; for(int i = 0 ;i < 100 ;i++) { int n = (int) (Math.random()*101); sum += n; } System.out.println("平均值為:" + (sum/100.0)); }
二: 輸入一個100-999的數,判斷它是不是水仙花數
public static void main(String[] args) throws MyException { System.out.println("請輸入一個大於100小於1000的三位整數:"); Scanner sc = new Scanner(System.in); int input = sc.nextInt(); if(input < 100 || input > 1000) { throw new MyException();//自訂異常類,判斷輸入的資料是否滿足條件 } int a = input / 100; int b = input / 10 % 10; int c = input % 10; if (input == a * a * a + b * b * b + c * c * c) { System.out.print(input + "是水仙花數" + "\n" + input + "=" + (a + "*" + a +"*"+ a) + "+" +(b+ "*"+ b +"*" +b) +"+"+ (c +"*"+ c +"*"+ c)); } else System.out.print(input + "不是水仙花數"); }
三:判斷一個數是奇數還是偶數
public static void main(String[] args) { //從鍵盤接收資料 System.out.println("請輸入一個整數:"); Scanner s = new Scanner(System.in); int x = s.nextInt(); int a = x % 2; if(a != 0) { System.out.println(x + "是奇數"); } else { System.out.println(x + "是偶數"); } }
四:升學考試問題:英語,數學C語言三門總成績大於等於230,並且英語成績大於等於60才能升學
public static void main(String[] args) { //升學考試問題:英語,數學C語言三門總成績大於等於230,並且英語成績大於等於60 //才能升學 System.out.print("請輸入你的英語成績:"); Scanner english = new Scanner(System.in); double x = english.nextDouble(); System.out.print("請輸入你的數學成績:"); Scanner math = new Scanner(System.in); double y = english.nextDouble(); System.out.print("請輸入你的C語言成績:"); Scanner cEnglish = new Scanner(System.in); double z = cEnglish.nextDouble(); //三門課程總成績 double sum = x + y + z; System.out.println("英語成績為" + x); System.out.println("三門課程總成績" + sum); if(x >= 60 && sum >= 230) { System.out.println("恭喜你!成功升學!!"); }else { System.out.println("很遺憾!你還需要加油哦!!"); } }
五:輸入一個數,判斷是否能被3整出,並輸出相應的提示資訊
public static void main(String[] args) { //輸入一個數,判斷是否能被3整出,並輸出相應的提示資訊 System.out.print("請輸入一個數:"); Scanner a = new Scanner(System.in); double x = a.nextDouble(); if((x % 3) == 0) { System.out.println(x + "能被3整除"); }else { System.out.println(x + "不能被3整除"); } }
六:判斷兩個數的最大值並輸出
public static void main(String[] args) { //判斷兩個數的最大值並輸出 System.out.print("請輸入第一個數:"); Scanner a = new Scanner(System.in); double x = a.nextDouble(); System.out.print("請輸入第二個數:"); Scanner b = new Scanner(System.in); double y = b.nextDouble(); //三目運算子 double z = (x > y)?x:y; System.out.println("兩個數的最大值為:" + z); }
七:用if-else語句判斷輸入的年份是否為閏年
能被4整除但不能被100整除的年份,或者能被400整除的年份
public static void main(String[] args) { //用if-else語句判斷輸入的年份是否為閏年 //能被4整除但不能被100整除的年份,或者能被400整除的年份 System.out.print("請輸入一個年份:"); Scanner sc = new Scanner(System.in); int year = sc.nextInt(); if(((year % 4 ==0) && ((year & 100) != 0))||(year % 400 == 0)) { System.out.println(year + "是閏年"); }else { System.out.println(year + "不是閏年"); } }
八:鍵盤輸入1-7的任一數字,分別輸出對應的資訊
public static void main(String[] args) { //鍵盤輸入1-7的任一數字,分別輸出對應的資訊 System.out.print("請輸入1-7的任一數字:"); Scanner sc = new Scanner(System.in); int week = sc.nextInt(); switch(week) { case 1: System.out.println("今天是星期一"); break; case 2: System.out.println("今天是星期二"); break; case 3: System.out.println("今天是星期三"); break; case 4: System.out.println("今天是星期四"); break; case 5: System.out.println("今天是星期五"); break; case 6: System.out.println("今天是星期六"); break; case 7: System.out.println("今天是星期日"); break; default: System.out.println("您輸入的數字是無效的"); } sc.close(); }
Java_基礎文法編程練習