public class Double01 { /** * 若有3個班級各4名學員參賽, * 如何計算每個班級參賽學員的平均分? */ public static void main(String[] args) { // 創建一個2維數組保存數據 double[][] scores = new double[3][4]; // 循環輸入學員的成績 Scanner scanner = new Scanner(System.in); // 定義一個變量 保存每個班級的總分 double sum = 0; // 01.外層循環控制班級數 for (int i = 0; i < scores.length; i++) { System.out.println("請您輸入第" + (i + 1) + "個班級的學生成績************"); // 02.內層循環控制學生人數 for (int j = 0; j < scores[i].length; j++) { System.out.println("請您輸入第" + (j + 1) + "個學生成績"); scores[i][j] = scanner.nextDouble(); sum += scores[i][j]; } System.out.println("第" + (i + 1) + "個班級的平均分是:" + (sum / scores[i].length)); // 總分清零 為了下個班級的總分計算 sum = 0; } } }
public class Double02 { /** * 直角三角形 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請您輸入需要顯示的行數:"); int num = scanner.nextInt(); // 外層循環控制行數 for (int i = 1; i <= num; i++) { for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); // 換行 } System.out.println("========================"); // 倒三角 for (int i = 1; i <= num; i++) { for (int j = 1; j <= num + 1 - i; j++) { System.out.print("*"); } System.out.println(); // 換行 } } }
public class Double03 { /** * 九九乘法表 */ public static void main(String[] args) { // 外層循環控制行數 for (int i = 1; i <= 9; i++) { // 內層循環控制列 j<=i for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + (i * j) + "\t"); } System.out.println(); } } }
public class Double04 { /** * 冒泡排序 * 01.外層循環N-1 (N:數組的長度) * 02.內層循環N-1-i (i:比較的輪數,外層循環的變量) * 03.兩兩相比 小靠前!必須進行等量轉換!互換位置! */ public static void main(String[] args) { int[] nums = { 80, 20, 25, 30, 1, 8, 100, 72, 50 }; // 外層循環控制輪數 for (int i = 0; i < nums.length - 1; i++) { // 內層循環控制比較次數 for (int j = 0; j < nums.length - 1 - i; j++) { // 兩兩相比 小靠前! 如果后面的小,需要和前面的數據互換位置 if (nums[j + 1] < nums[j]) { int temp = 0; temp = nums[j + 1]; nums[j + 1] = nums[j]; nums[j] = temp; } } } System.out.println("排序之后的數組:" + Arrays.toString(nums)); } }
public class Double05 { /** * 若有3個班級各4名學員參賽, * 如何計算每個班級參賽學員的平均分? * 計算成績大於85分的學生數量 */ public static void main(String[] args) { // 創建一個2維數組保存數據 double[][] scores = new double[3][4]; // 循環輸入學員的成績 Scanner scanner = new Scanner(System.in); // 定義一個變量保存 大於85分的學生數量 int count = 0; // 01.外層循環控制班級數 for (int i = 0; i < scores.length; i++) { System.out.println("請您輸入第" + (i + 1) + "個班級的學生成績************"); // 02.內層循環控制學生人數 for (int j = 0; j < scores[i].length; j++) { System.out.println("請您輸入第" + (j + 1) + "個學生成績"); scores[i][j] = scanner.nextDouble(); // 獲取學生成績 if (scores[i][j] < 85) { continue; // 如果分數少於85 不計算次數 } count++; } } System.out.println("85分以上的學生人數是:" + count); } }
public class Double07 { /** * 有3名顧客去商場購物,每人買3件商品 商品單價300元以上的商品享受8折優惠 請統計每人享受打折優惠的商品的數量 */ public static void main(String[] args) { // 保存顧客打折的商品數量 int count = 0; double price = 0; Scanner scanner = new Scanner(System.in); // 控制顧客的數量 for (int i = 1; i <= 3; i++) { System.out.println("請輸入第" + i + "名顧客購買的商品價格"); for (int j = 1; j <= 3; j++) { price = scanner.nextDouble(); if (price < 300) { continue; } count++; } System.out.println("第" + i + "名顧客打折的商品數量:" + count); count = 0; } } }

// 九九乘法表 外層循環每執行一次,內層循環執行一遍 for (int i = 1; i <= 9; i++) { // 外層控制的是行數 for (int j = 1; j <= i; j++) { // 內層控制的是列數 System.out.print(i + "*" + j + "=" + (i * j) + "\t"); } System.out.println();// 換行 }

// 正三角形 for (int i = 1; i <= 5; i++) { // 先輸出空格 for (int j = 1; j <= 5 - i; j++) { System.out.print(" "); } // 打印 * for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); }

// 正三角形 for (int i = 1; i <= 5; i++) { // 先輸出空格 for (int j = 1; j <= 5 - i; j++) { System.out.print(" "); } // 打印 * for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } // 倒三角形 for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { // 空格 System.out.print(" "); } for (int j = 1; j <= 9 - 2 * i; j++) { // * System.out.print("*"); } System.out.println(); }

// 打印平行四邊形 for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5 - i; j++) { // 空格 System.out.print(" "); } for (int j = 1; j <= 5; j++) { // * System.out.print("*"); } System.out.println(); }
=====================循環的小練習================================

@Test public void test01() { /** * 循環錄入某學生5門課的成績並計算平均分, * 如果某分數錄入為負,停止錄入並提示錄入錯誤 */ Scanner scanner = new Scanner(System.in); System.out.println("請輸入您的姓名:"); String name = scanner.next(); double sum = 0; // 循環獲取用戶的成績 for (int i = 1; i <= 3; i++) { System.out.println("請輸入第" + i + "門課程的成績:"); double score = scanner.nextDouble(); // 如果輸入了負數 則跳出循環體 if (score < 0) { System.out.println("您的輸入有誤!"); break; } sum += score; // 計算總成績 } // 對數字進行格式化 DecimalFormat df = new DecimalFormat("#.00"); System.out.println("平均分為:" + df.format((sum / 3))); } @Test // 10以內的整數相加,值大於20的時候輸出並且跳出循環 public void test02() { int sum = 0; for (int i = 1; i <= 10; i++) { sum += i; if (sum > 20) { System.out.println(sum); break; } } } /** * 循環錄入Java課的學生成績, * 統計分數大於等於80分的學生比例 */ @Test public void test03() { Scanner scanner = new Scanner(System.in); System.out.println("請您輸入班級的人數:"); int total = scanner.nextInt(); // 總人數 double score = 0; // 成績 int sum = 0;// 記錄大於80分的人數 for (int i = 1; i <= total; i++) { // 循環錄入成績 System.out.println("請輸入第" + i + "門課程的成績:"); score = scanner.nextDouble(); if (score >= 80) { // 如果成績大於80 sum++ sum++; continue; // 結束本次循環 執行i++語句 繼續執行下次的循環 } System.out.println("小於80分 才能執行這句話!"); } // 輸出百分比 sum(大於80)/total(總人數) DecimalFormat df = new DecimalFormat("#.00%"); System.out.println("80分以上的百分比是:" + df.format(sum / (total + 0.0))); } @Test // 1-10之間的偶數和 public void test04() { int sum = 0; for (int i = 1; i <= 10; i++) { if (i % 2 != 0) { // 奇數就要不相加 continue; } sum += i; } System.out.println("偶數和是:" + sum); } @Test // 雙重循環中的 continue public void test05() { for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { break; } System.out.println("j" + j); } } Scanner scanner = new Scanner(System.in); String next = scanner.next(); System.out.println(next); }