判斷某一年是否為閏年
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入一個年份:"); //向控制台輸出一個提示信息
long year;
try {
year = scan.nextLong();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // 是閏年
System.out.print(year + "是閏年!");
} else { // 不是閏年
System.out.print(year + "不是閏年!");
}
} catch (Exception e) {
System.out.println("您輸入的不是有效的年份!");
}
}
}
結果:

驗證登錄信息的合法性
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);// 創建掃描器
System.out.println("請輸入登錄用戶名:");
String username = scan.nextLine();// 接收用戶輸入登錄名
System.out.println("請輸入登錄密碼:");
String password = scan.nextLine();// 接收用戶輸入登錄密碼
if (!username.equals("mr")) {// 判斷用戶名合法性
System.out.println("用戶名非法。");
} else if (!password.equals("mrsoft")) {// 判斷密碼合法性
System.out.println("登錄密碼錯誤。");
} else {// 通過以上兩個條件判斷則默認通過登錄驗證
System.out.println("恭喜您,登錄信息通過驗證。");
}
}
}
結果


為新員工分配部門
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入新員工的姓名:");
String name = scan.nextLine();// 接收員工名稱
System.out.println("請輸入新員工應聘的編程語言:");
String language = scan.nextLine();// 接收員工應聘的編程語言
// 根據編程語言確定員工分配的部門
switch (language.hashCode()) {
case 3254818:// java的哈希碼
case 2301506:// Java的哈希碼
case 2269730:// JAVA的哈希碼
System.out.println("員工"+name+"被分配到Java程序開發部門。");
break;
case 3104:// c#的哈希碼
case 2112:// C#的哈希碼
System.out.println("員工"+name+"被分配到C#項目維護組。");
break;
case -709190099: // asp.net的哈希碼
case 955463181: // Asp.net的哈希碼
case 9745901: // ASP.NET的哈希碼
System.out.println("員工"+name+"被分配到Asp.net程序測試部門。");
break;
default:
System.out.println("本公司不需要" + language + "語言的程序開發人員。");
}
}
}

用switch語句根據消費金額計算折扣
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
float money = 1170; // 金額
String rebate = ""; // 折扣
if (money > 200) {
int grade = (int) money / 200; // 等級
switch (grade) { // 根據等級計算折扣比例
case 1:
rebate = "九五折";
break;
case 2:
rebate = "九折";
break;
case 3:
rebate = "八五折";
break;
case 4:
rebate = "八三折";
break;
case 5:
rebate = "八折";
break;
case 6:
rebate = "七八折";
break;
case 7:
rebate = "七五折";
break;
case 8:
rebate = "七三折";
break;
case 9:
rebate = "七折";
break;
case 10:
rebate = "六五折";
break;
default:
rebate = "六折";
}
}
System.out.println("您的累計消費金額為:" + money);// 輸出消費金額
System.out.println("您將享受" + rebate + "優惠!"); // 輸出折扣比例
}
}
結果:

判斷用戶輸入月份的季節
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // 創建掃描器
// 提示用戶輸入月份
System.out.println("請輸入一個月份,我能告訴你它屬於哪個季節。");
int month = scan.nextInt(); // 接收用戶輸入
switch (month) { // 判斷月份屬於哪個季節
case 12:
case 1:
case 2:
System.out.print("您輸入的月份屬於冬季。");
break;
case 3:
case 4:
case 5:
System.out.print("您輸入的月份屬於春季");
break;
case 6:
case 7:
case 8:
System.out.print("您輸入的月份屬於夏季");
break;
case 9:
case 10:
case 11:
System.out.print("您輸入的月份屬於秋季");
break;
default:
System.out.print("你那有" + month + "月份嗎?");
}
}
}
結果

使用while循環語句與自增運算符循環遍歷數組
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
// 創建鳥類數組
String[] aves = new String[] { "白鷺", "黃鸝", "鸚鵡", "烏鴉", "喜鵲",
"布谷鳥", "斑鳩", "百靈鳥" };
int index = 0;// 創建索引變量
System.out.println("我的花園里有很多鳥,大約包括:");
while (index < aves.length) {// 遍歷數組
System.out.println(aves[index++]);// 自增索引值
}
}
}
結果:

使用for循環輸出楊輝三角形
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
int triangle[][]=new int[10][];// 創建二維數組
// 遍歷二維數組的第一層
for (int i = 0; i < triangle.length; i++) {
triangle[i]=new int[i+1];// 初始化第二層數組的大小
// 遍歷第二層數組
for(int j=0;j<=i;j++){
// 將兩側的數組元素賦值為1
if(i==0||j==0||j==i){
triangle[i][j]=1;
}else{// 其他數值通過公式計算
triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
}
System.out.print(triangle[i][j]+"\t"); // 輸出數組元素
}
System.out.println(); //換行
}
}
}
結果:

使用嵌套循環在控制台上輸出九九乘法表
package com;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
for(int i=1;i<=9;i++){// 循環控制變量從1遍歷到9
for(int j=1;j<=i;j++){// 第二層循環控制變量與第一層最大索引相等
// 輸出計算結果但不換行
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();// 在外層循環中換行
}
}
}
結果:

使用while循環計算1+1/2!+1/3!…1/20!
package com;
import java.math.BigDecimal;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
BigDecimal sum = new BigDecimal(0.0); // 和
BigDecimal factorial = new BigDecimal(1.0); // 階乘項的計算結果
int i = 1; // 循環增量
while (i <= 20) {
sum = sum.add(factorial); // 累加各項階乘的和
++i; // i加1
factorial = factorial.multiply(new BigDecimal(1.0 / i)); // 計算階乘項
}
System.out.println("1+1/2!+1/3!···1/20!的計算結果等於:\n" + sum); // 輸出計算結果
}
}
結果:

使用for循環輸出空心的菱形
package com;
import java.math.BigDecimal;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
printHollowRhombus(6);
}
public static void printHollowRhombus(int size) {
if (size % 2 == 0) {
size++;// 計算菱形大小
}
for (int i = 0; i < size / 2 + 1; i++) {
for (int j = size / 2 + 1; j > i + 1; j--) {
System.out.print(" ");// 輸出左上角位置的空白
}
for (int j = 0; j < 2 * i + 1; j++) {
if (j == 0 || j == 2 * i) {
System.out.print("* ");// 輸出菱形上半部邊緣
} else {
System.out.print(" ");// 輸出菱形上半部空心
}
}
System.out.println(""); //換行
}
for (int i = size / 2 + 1; i < size; i++) {
for (int j = 0; j < i - size / 2; j++) {
System.out.print(" ");// 輸出菱形左下角空白
}
for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
if (j == 0 || j == 2 * (size - i - 1)) {
System.out.print("* ");// 輸出菱形下半部邊緣
} else {
System.out.print(" ");// 輸出菱形下半部空心
}
}
System.out.println(""); //換行
}
}
}
結果:

終止循環體
package com;
import java.math.BigDecimal;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
System.out.println("\n-------------中斷單層循環的例子-------------");
// 創建數組
String[] array = new String[] { "白鷺", "丹頂鶴", "黃鸝", "鸚鵡", "烏鴉", "喜鵲",
"老鷹", "布谷鳥", "老鷹", "灰紋鳥", "老鷹", "百靈鳥" };
System.out.println("在你發現第一只老鷹之前,告訴我都有什么鳥。");
for (String string : array) { // foreach遍歷數組
if (string.equals("老鷹")) // 如果遇到老鷹
break;// 中斷循環
System.out.print("有:" + string+" "); // 否則輸出數組元素
}
System.out.println("\n\n-------------中斷雙層循環的例子-------------");
// 創建成績數組
int[][] myScores = new int[][] { { 67, 78, 63, 22, 66 }, { 55, 68, 78, 95, 44 }, { 95, 97, 92, 93, 81 } };
System.out.println("寶寶這次考試成績:\n數學\t語文\t英語\t美術\t歷史");
No1: for (int[] is : myScores) { // 遍歷成績表格
for (int i : is) {
System.out.print(i + "\t"); // 輸出成績
if (i < 60) { // 如果中途遇到不及格的,立刻中斷所有輸出
System.out.println("\n等," + i + "分的是什么?這個為什么不及格?");
break No1;
}
}
System.out.println();
}
}
}
結果:

循環體的過濾器 :
package com;
import java.math.BigDecimal;
import java.util.Scanner;
public class Msj {
public static void main(String[] args) {
// 創建數組
String[] array = new String[] { "白鷺", "丹頂鶴", "黃鸝", "鸚鵡", "烏鴉", "喜鵲",
"老鷹", "布谷鳥", "老鷹", "灰紋鳥", "老鷹", "百靈鳥" };
System.out.println("在我的花園里有很多鳥類,但是最近來了幾只老鷹,請幫我把它們抓走。");
int eagleCount = 0;
for (String string : array) {// foreach遍歷數組
if (string.equals("老鷹")) {// 如果遇到老鷹
System.out.println("發現一只老鷹,已經抓到籠子里。");
eagleCount++;
continue;// 中斷循環
}
System.out.println("搜索鳥類,發現了:" + string);// 否則輸出數組元素
}
System.out.println("一共捉到了:" + eagleCount + "只老鷹。");
}
}
結果:

