1、 編寫程序:從鍵盤上讀入一個學生成績, 存放在變量score中,根據score的值輸出其對應的成績等級: score>=90 等級: A 70=<score<90 等級: B 60=<score<70 等級: C score<60 等級: D
import java.util.Scanner; public class Course{ public static void main(String []arg){ Scanner scanner = new Scanner(System.in); System.out.print("請輸入學生的學習成績, score="); int score = scanner.nextInt(); switch(score/10){ case 10: case 9: System.out.println("A"); break; case 8: case 7: System.out.println("B"); break; case 6: System.out.println("C"); break; default: System.out.println("D"); } } }
2、編寫程序:由鍵盤輸入三個整數分別存入變量num1、num2、num3,
對它們進行排序(使用 if-else if-else),並且從小到大輸出。
import java.util.Scanner; public class Course{ public static void main(String []arg){ System.out.print("請輸入三個數字:"); Scanner scanner = new Scanner(System.in); System.out.print("第一個數字:num1 = "); int num1 = scanner.nextInt(); System.out.print("第二個數字:num2 = "); int num2 =scanner.nextInt(); System.out.print("第三個數字:num3 = "); int num3 =scanner.nextInt(); if(num1<=num2){ if(num3<=num1){
System.out.println("您輸入的三個數字從小到大排序為:"
+num3+" , "+num1+" , "+num2);}
else if(num3>=num2){
System.out.println("您輸入的三個數字從小到大為:"
+num1+" , "+num2+" , "+num3);} else{
System.out.println("您輸入的三個數字從小到大為:"
+num1+" , "+num3+" , "+num2);} }else{ if(num3<=num2){System.out.println("您輸入的三個數字從小到大排序為:"
+num3+" , "+num2+" , "+num1);} else if(num3>=num1){System.out.println("您輸入的三個數字從小到大為:"
+num2+" , "+num1+" , "+num3);} else{System.out.println("您輸入的三個數字從小到大為:"
+num2+" , "+num3+" , "+num1);} } } }
3、輸入 2013 年的某個月份和日期, 例如 month=4, day=21
經過程序計算, 打印出輸入的月份和日期是 2013 年的第幾天. 31+28+31+21
import java.util.Scanner; public class TestSwitch{ public static void main(String [] args){ System.out.print("month:"); int month = scanner.nextInt(); System.out.print("day:"); int day = scanner.nextInt(); int days = day; switch(month - 1){ case 11: days += 30; case 10: days += 31; case 9: days += 30; case 8: days += 31; case 7: days += 31; case 6: days += 30; case 5: days += 31; case 4: days += 30; case 3: days += 31; case 2: days += 28; case 1: days += 31; } System.out.println(month + "月" + day + "日是 2013 年的第" + days + "天"); } }
4、從鍵盤讀入個數不確定的整數,並判斷讀入的正數和負數的個數,輸入為0時結束輸入。
import java.util.Scanner; public class Demo1{ public static void main(String [] args){ Scanner scanner = new Scanner(System.in); //標記正數的個數 int x = 0; //標記負數的個數 int y = 0; while(true){ int b = scanner.nextInt(); if(b > 0){ x++; }else if(b < 0){ y++; }else{ //退出循環使用 break 語句. break; } } } }
5.(1)從鍵盤讀入學生成績,找出最高分,並輸出學生成績等級。 成績>=最高分-10 等級為’A’ 成績>=最高分-20 等級為’B’ 成績>=最高分-30 等級為’C’ 其余 等級為’D’ 提示:先讀入學生人數,根據人數創建int數組,存放學生成績。
(2)使用二維數組打印 10 行的楊輝三角。
package com.atguigu.java; import java.util.Scanner; public class TestArray { public static void main(String[] args) { //1. 數組的聲明 //int i = 0; int [] a = null; //推薦使用此種方式進行聲明. int b [] = null; //注意: Java語言中聲明數組時不能指定其長度(數組中元素的數) //int [5] c = null; //注意: 若沒有為數組變量分配指向的內存空間, 就調用其屬性, 會在運行時發生 "空指針異常" //System.out.println(a.length); //2. 為數組分配內存空間 a = new int[10]; //3. 獲取數組的長度 System.out.println(a.length); //10 //5. 對數組元素進行初始化 for(int i = 0; i < a.length; i++){ a[i] = 100 + i; } /*4. 訪問數組的元素: 數組名[數組元素下標]. 注意: 下標從 0 開始,
所以最大值是 lengh - 1, 而不是length;*/ //數組元素會進行默認的初始化: for(int i = 0; i < a.length; i++){ System.out.println(a[i]); } //若訪問數組的下標超過合法范圍, 則在運行時會拋出 ArrayIndexOutOfBoundsException //a[10] = 10; //數組的靜態初始化 int [] c = {1, 2, 3, 4, 5}; int [] d = new int[]{2, 3, 4, 5, 6}; for(int i = 0; i < d.length; i++){ System.out.println(d[i]); } //聲明一個二維數組 int [][] aa = new int[5][]; //對二維數組的元素進行初始化: 二維數組的元素是一個一維數組! //遍歷需要使用嵌套的 for 循環. for(int i = 0; i < aa.length; i++){ aa[i] = new int[i + 1]; for(int j = 0; j < aa[i].length; j++){ aa[i][j] = 1 + i * j; } } //對二維數組進行遍歷 for(int i = 0; i < aa.length; i++){ for(int j = 0; j < aa[i].length; j++){ System.out.print(aa[i][j] + " "); } System.out.println(); } /** * 2.從鍵盤讀入學生成績,找出最高分,並輸出學生成績等級。 * 成績>=最高分-10 等級為’A’ 成績>=最高分-20 等級為’B’ * 成績>=最高分-30 等級為’C’ 其余 等級為’D’ * 提示:先讀入學生人數,根據人數創建int數組,存放學生成績。 */ //1. 創建 Scanner 類 Scanner scanner = new Scanner(System.in); //2. 讀入要錄入的學生人數 System.out.print("請輸入學生人數"); int count = scanner.nextInt(); //3. 創建一個 int 類型的數組, 用於存放學員的成績, 數組的長度為 2 所錄入數值 int [] scores = new int[count]; //4. 利用循環錄入學生的成績, 同時得到最高分. 把學生成績錄入到 3 聲明的數組中. int highScore = 0; for(int i = 0; i < scores.length; i++){ scores[i] = scanner.nextInt(); if(scores[i] > highScore){ highScore = scores[i]; } } //5. 遍歷 3 聲明的數組, 根據最高分, 獲取學生的升級等級. for(int i = 0; i < scores.length; i++){ if(scores[i] >= highScore - 10){ System.out.println("student " + i + " score is " + scores[i] + " grade is A"); }else if(scores[i] >= highScore - 20){ System.out.println("student " + i + " score is " + scores[i] + " grade is B"); }else if(scores[i] >= highScore - 30){ System.out.println("student " + i + " score is " + scores[i] + " grade is C"); }else{ System.out.println("student " + i + " score is " + scores[i] + " grade is D"); } } //作業: 使用二維數組打印 10 行的楊輝三角, 楊輝三角實際上是二項式展開式的系數 /** * 1 (a+b)^0 * 1 1 (a+b)^1 * 1 2 1 (a+b)^2 * 1 3 3 1 (a+b)^3 * 1 4 6 4 1 * 1 5 10 10 5 1 * * ... * * 1. 什么是楊輝三角: 二項式展開式的系數 * 2. 為什么使用的是 二維數組: 因為楊輝三角由行和列組成, 每一行是一個一維數組,
而楊輝三角則是有一維數組組成的數組, 即二維數組. * 3. 楊輝三角的具體特點: * 3.1 第 n 行有 n 個元素(n >= 1) * 3.2 每一行的第一個元素和最后一個元素都是 1 * 3.3 不等於 1 的那些元素的值為上一行的對應列和對應列的前一列的元素的和. */ //1. 先聲明一個二維數組: 二維數組有 10 個元素 int [][] yh = new int[10][]; //2. 對二維數組的元素進行初始化: 第 i 個元素(i 從 0 開始) 為長度為 i + 1 的一維數組. for(int i = 0; i < yh.length; i++){ //二維數組的每一個元素是一維數組, 而一維數組時引用類型, 其默認值為 null //System.out.println(yh[i]); yh[i] = new int[i + 1]; //3. 對具體的每一個元素進行初始化(是 1 的元素): yh[i][0] = 1, yh[i][i] = 1 yh[i][0] = 1; yh[i][i] = 1; //4. 對具體的每一個元素進行初始化(不是 1 的元素): yh[i][j] = yh[i-1][j] + yh[i-1][j-1]; // (i > 1 && j > 0 && j < i) if(i > 1){ for(int j = 1; j < i; j++){ yh[i][j] = yh[i-1][j] + yh[i-1][j-1]; } } } //打印 for(int i = 0; i < yh.length; i++){ for(int j = 0; j < yh[i].length; j++){ System.out.print(yh[i][j] + "\t"); } System.out.println(); } } }
6、打印 1-100 之間的所有素數
package com.atguigu.java; public class HelloWorld { public static void main(String[] args) { System.out.println("hello eclipse..."); //打印 1-100 之間的所有素數 //素數: 有且僅有兩個正約數的整數. 即 2 ~ i-1 之間沒有一個是其約數. //標記一個整數是素數. true 表示是一個素數, false 表示不是素數. boolean flag = true; long begin = System.currentTimeMillis(); for(int i = 2; i <= 100000; i++){ flag = true; /** * 16 * * 2 8 * 3 * 4 4 * 5 * 7 * 8 2 * * */ //從 2 循環到 i-1, 檢驗每一個數是否為 i 的約數. for(int j = 2; j <= Math.sqrt(i); j++){ if(i % j == 0){ flag = false; //結束這次循環. break; } } if(flag){ System.out.println(i); } } long end = System.currentTimeMillis(); System.out.println("time: " + (end - begin)); //2194 98 } }
