java流程式控制制,java流程

來源:互聯網
上載者:User

java流程式控制制,java流程

判斷某一年是否為閏年

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 + "只老鷹。");
}
}

結果:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.