1.條件分支
1.1 單條件分支
語法結(jié)構(gòu):
if(邏輯運(yùn)算){
一段代碼
}
當(dāng)邏輯運(yùn)算結(jié)果為true時(shí),執(zhí)行{}中的代碼
當(dāng)邏輯運(yùn)算結(jié)果為false時(shí),什么都不做
如果"一段代碼"只有一行,可以寫成(省略花括號)
if(邏輯運(yùn)算)
一行代碼
示例1:如果張浩的Java考試成績大于98分,張浩就能獲得一個MP4作為獎勵
public class SimpleIf {
public static void main(String[] args) {
int score = 91; //張浩的Java成績
if ( score > 98 ) {
System.out.println("老師說:不錯,獎勵一個MP4!");
}
}
}
1.2 多條件分支
由多個if組成的分支,每個if都是獨(dú)立的
public class SimpleIf {
public static void main(String[] args) {
int score = 91; //張浩的Java成績
if ( score > 98 ) {
System.out.println("老師說:不錯,獎勵一個MP4!");
}
if ( score <= 98 ) {
System.out.println("老師說:懲罰進(jìn)行編碼!");
}
}
}
1.3 互斥條件分支(二選一)
語法結(jié)構(gòu):
if(邏輯運(yùn)算){
一段代碼
}else{
一段代碼
}
if...else...兩個分支一定會執(zhí)行其中一個
示例3:判斷輸入的數(shù)字是否為偶數(shù)
/**
* 使用if-else結(jié)構(gòu)實(shí)現(xiàn)幸運(yùn)抽獎
*/
public class GoodLuck {
public static void main(String[] args) {
/* 產(chǎn)生隨機(jī)數(shù) */
int random = (int) (Math.random() * 10);
/* 從控制臺接收一個4位會員號 */
System.out.println("我行我素購物管理系統(tǒng) > 幸運(yùn)抽獎\n");
System.out.print("請輸入4位會員號: ");
Scanner input = new Scanner(System.in);
int custNo = input.nextInt();
/* 分解獲得百位 */
int baiwei = custNo / 100 % 10;
/* 判斷是否是幸運(yùn)會員 */
if (baiwei == random) {
System.out.println(custNo + "是幸運(yùn)客戶,獲精美Mp3一個。");
} else {
System.out.println(custNo + " 謝謝您的支持!");
}
}
}
1.4 多條件互斥條件分支(多選一)
1.4.1 if...else if...else...
語法結(jié)構(gòu)
if(邏輯運(yùn)算1){
一段代碼
}else if(邏輯運(yùn)算2){
一段代碼
} ...
}else if(邏輯運(yùn)算n){
一段代碼
}else{
一段代碼
}
if和else..if和else組成了多個分支,程序只能進(jìn)入第一個符合條件的分支
public class CalcDiscount2 {
public static void main(String[] args){
/* 輸入會員積分 */
System.out.print("請輸入會員積分: ");
Scanner input = new Scanner(System.in);
int custScore = input.nextInt();
double discount;
/* 判斷折扣 */
if (custScore < 2000) {
discount = 0.9;
} else if ( custScore>=2000 && custScore < 4000) {
discount = 0.8;
} else if ( custScore>=4000 && custScore < 8000) {
discount = 0.7;
} else {
discount = 0.6;
}
System.out.println("該會員享受的折扣是:" + discount);
}
}
1.4.2 switch(n)...case...語法
示例4:顯示我行我素購物管理系統(tǒng)的登錄菜單
public class LoginMenu {
/**
* 顯示我行我素購物管理系統(tǒng)的登錄菜單
*/
public static void main(String[] args) {
System.out.println("\n\t\t歡迎使用我行我素購物管理系統(tǒng)1.0版\n");
System.out.println("\t\t\t 1\. 登 錄 系 統(tǒng)\n");
System.out.println("\t\t\t 2\. 退 出\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * *\n");
System.out.print("請選擇,輸入數(shù)字:");
/* 從鍵盤獲取信息,并執(zhí)行相應(yīng)操作---新加代碼 */
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num) {
case 1:
/* 顯示系統(tǒng)主菜單 */
System.out.println("\n\t\t歡迎使用我行我素購物管理系統(tǒng)\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * \n");
System.out.println("\t\t\t\t 1\. 客 戶 信 息 管 理\n");
System.out.println("\t\t\t 2\. 購 物 結(jié) 算\n");
System.out.println("\t\t\t 3\. 真 情 回 饋\n");
System.out.println("\t\t\t 4\. 注 銷\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
System.out.print("請選擇,輸入數(shù)字:");
break;
case 2:
/* 退出系統(tǒng) */
System.out.println("謝謝您的使用!");
break;
default:
System.out.println("輸入錯誤。");
break;
}
}
}
如果符合某個分支,從某個分支開始執(zhí)行
如果某個case分支不寫break,繼續(xù)執(zhí)行下一個分支,直至遇到break或者所有分支全都執(zhí)行完畢。
swith句式不能用于區(qū)間的判斷,switch(n)中n的數(shù)據(jù)類型是有限制的,常規(guī)意義上只能是“byte,short,int,char”,當(dāng)前的JDK1.8版本又支持了"String"類型
嵌套if
什么時(shí)候使用嵌套if?
例如:
學(xué)校舉行運(yùn)動會,百米賽跑跑入10秒內(nèi)的學(xué)生有資格進(jìn)決賽,根據(jù)性別分別進(jìn)入男子組和女子組
分析:
1.要判斷是否能夠進(jìn)入決賽
2.在確定進(jìn)入決賽的情況下,還要判斷是進(jìn)入男子組,還是進(jìn)入女子組
if(score<=10){
if(gender.equals("男")){
System.out.println("進(jìn)入男子組決賽!");
}else if(gender.equals("女")){
System.out.println("進(jìn)入女子組決賽!");
}
}else{
System.out.println("淘汰!");
}
例如:
public class CalcDiscount {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("請輸入是否是會員:是(y)/否(其他字符)");
String identity = input.next();
System.out.println("請輸入購物金額:");
double money = input.nextDouble();
if(identity.equals("y")){ //會員
if(money>200){
money = money * 0.75;
}else{
money = money * 0.8;
}
}else{ //非會員
if(money>100){
money = money * 0.9;
}
}
System.out.println("實(shí)際支付:" + money);
}
}
綜合應(yīng)用
題目:

public class ConsumeSavePlan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
Scanner input = new Scanner(System.in);
System.out.print("請輸入消費(fèi)金額:");
double money = input.nextDouble();
double extra = 0; //換購付款
int choice = 0; //換購項(xiàng)目
if(money>=50){
System.out.println("是否參加優(yōu)惠換購活動:");
System.out.println("1:滿50元,加2元換購百事可樂飲料1瓶");
System.out.println("2:滿100元,加3元換購500ml可樂一瓶");
System.out.println("3:滿100元,加10元換購5公斤面粉");
System.out.println("4:滿200元,加10元可換購1個蘇波爾炒菜鍋");
System.out.println("5:滿200元,加20元可換購歐萊雅爽膚水一瓶");
System.out.println("0:不換購");
System.out.print("請選擇:");
if(input.hasNextInt()==true){
choice = input.nextInt();
switch(choice){
case 1:
if(money>=50){
extra = 2;
}
break;
case 2:
if(money>=100){
extra = 3;
}
break;
case 3:
if(money>=100){
extra = 10;
}
break;
case 4:
if(money>=200){
extra = 10;
}
break;
case 5:
if(money>200){
extra = 20;
}
break;
default:
break;
}
}else{
System.out.println("請輸入正確的數(shù)字!");
}
}
//結(jié)賬
double total = money + extra;
System.out.println("本次消費(fèi)總金額:"+ total);
if(choice == 1){
System.out.println("成功換購:" + "百事可樂飲料1瓶。");
}else if(choice ==2 ){
System.out.println("成功換購:" + "500ml可樂一瓶。");
}else if(choice == 3){
System.out.println("成功換購:" + "5公斤面粉。");
}else if(choice == 4){
System.out.println("成功換購:" + "1個蘇波爾炒菜鍋。");
}else if(choice == 5){
System.out.println("成功換購:" + "歐萊雅爽膚水一瓶。");
}else {
System.out.println("無換購項(xiàng)目!");
}
}
}
2.循環(huán)
簡單理解:一件事重復(fù)做很多次,一段代碼重復(fù)執(zhí)行了N次
復(fù)雜理解:一段代碼重復(fù)執(zhí)行了N次,其中的變量每次都有變化
2.1 循環(huán)的要素
(1) 循環(huán)的開始邊界
(2) 循環(huán)的結(jié)束邊界
(3) 循環(huán)的增量
或
(1) 循環(huán)的條件
(2) 循環(huán)的增量
2.2 for循環(huán)
遵循規(guī)則:
(1) 循環(huán)的開始邊界
(2) 循環(huán)的結(jié)束邊界
(3) 循環(huán)的增量
適用于有明確循環(huán)次數(shù)的代碼(執(zhí)行多少次)
語法規(guī)則:
for(int i = 1; i <= 50; i++){
需要重復(fù)執(zhí)行的代碼
}
示例1:顯示50以內(nèi)所有符合“敲7”規(guī)則的數(shù)字
for(int i = 1; i <= 50; i++) {
int g = i % 10; //個位
int s = i / 10 ; //十位
if(i % 7 == 0 || (g == 7 || s == 7)) {
System.out.print(i);
System.out.print("\t");
}
}
for循環(huán)的執(zhí)行順序

for的死循環(huán)
- 沒有開始邊界,結(jié)束邊界和增量
for(;;) {
}
- 沒有增量
for(int i = 0; i <= 3;) {
}
- 沒有結(jié)束邊界
for(int i = 0;;i++) {
}
2.3 while循環(huán)/do...while循環(huán)
2.3.1 while循環(huán)
遵循規(guī)則:
(1) 循環(huán)的條件
(2) 循環(huán)的增量
適用于有明確循環(huán)條件的代碼(什么情況下執(zhí)行)
語法規(guī)則:
while(邏輯表達(dá)式){
需要重復(fù)執(zhí)行的代碼
}
當(dāng)邏輯表達(dá)式為true時(shí),會一直執(zhí)行{}中的代碼
示例2:商品價(jià)格查詢
public class PriceLookup {
/**
* 商品價(jià)格查詢
*/
public static void main(String[] args) {
String name = ""; //商品名稱
double price = 0.0; //商品價(jià)格
int goodsNo = 0; //商品編號
System.out.println("MyShopping管理系統(tǒng) > 購物結(jié)算\n");
//商品清單
System.out.println("*******************************************");
System.out.println("請選擇購買的商品編號:");
System.out.println("1.T 恤 2.網(wǎng)球鞋 3.網(wǎng)球拍");
System.out.println("*******************************************");
Scanner input = new Scanner(System.in);
String answer = "y"; //標(biāo)識是否繼續(xù)
while("y".equals(answer)){
System.out.print("請輸入商品編號:");
goodsNo = input.nextInt();
switch(goodsNo){
case 1:
name = "T 恤";
price = 245.0;
break;
case 2:
name = "網(wǎng)球鞋";
price = 570.0;
break;
case 3:
name = "網(wǎng)球拍";
price = 320.0;
break;
}
System.out.println(name+ "\t" + "¥" + price +"\n");
System.out.print("是否繼續(xù)(y/n)");
answer = input.next();
}
System.out.println("程序結(jié)束!");
}
}
將敲7程序改成while循環(huán)實(shí)現(xiàn)
int i = 1;
while(i <= 50) {
int g = i % 10; //個位
int s = i / 10 ; //十位
if(i % 7 == 0 || (g == 7 || s == 7)) {
System.out.print(i);
System.out.print("\t");
}
i++;
}
2.3.2 do...while循環(huán) (了解)
語法規(guī)則
do{
重復(fù)執(zhí)行的代碼
}while(邏輯表達(dá)式);
例如:
public class MainMenu {
/**
* 升級菜單切換
*/
public static void main(String[] args) {
System.out.println("歡迎使用MyShopping管理系統(tǒng)\n");
System.out.println("*******************************");
System.out.println("\t1.客 戶 信 息 管 理");
System.out.println("\t2.購 物 結(jié) 算");
System.out.println("\t3.真 情 回 饋");
System.out.println("\t4.注 銷");
System.out.println("*******************************\n");
int choice; //用戶選擇
boolean isRight; //輸入是否正確
System.out.print("請選擇,輸入數(shù)字:");
Scanner input = new Scanner(System.in);
do{
isRight = true;
choice = input.nextInt();
if(choice == 1){
System.out.println("執(zhí)行客戶信息管理");
}else if(choice == 2){
System.out.println("執(zhí)行購物結(jié)算");
}else if(choice == 3){
System.out.println("執(zhí)行真情回饋");
}else if(choice == 4){
System.out.println("執(zhí)行注銷");
}else{
System.out.print("輸入錯誤,請重新輸入數(shù)字:");
isRight = false;
}
}while(!isRight);
System.out.println("\n程序結(jié)束");
}
}
while與do...while的區(qū)別
while循環(huán),先判斷,再嘗試執(zhí)行??赡芤淮味疾粓?zhí)行。
do...while循環(huán),先執(zhí)行,再判斷。至少執(zhí)行一次
2.4 循環(huán)的控制
2.4.1 繼續(xù)(輪空) continue
某些情況下,我們希望多輪循環(huán)中,某輪循環(huán)輪空,使用continue
示例3: 打印[1,10]的所有整數(shù),不包括能被3整除的數(shù)字
for(int i = 1; i <= 10; i++) {
if(i % 3 == 0) {
continue;
}
System.out.println(i);
}
2.4.2 中斷(結(jié)束) break
某些情況下,我們希望循環(huán)在中途結(jié)束整個循環(huán)
示例4: 打印[1,10]的所有整數(shù),直到遇到第一個能被3整除的數(shù)字
for(int i = 1; i <= 10; i++) {
if(i % 3 == 0) {
break;
}
System.out.println(i);
}
continue與break的區(qū)別?
continue:結(jié)束該輪循環(huán),繼續(xù)執(zhí)行下一輪循環(huán)
break: 結(jié)束整個循環(huán)
2.4.3 循環(huán)嵌套
當(dāng)兩個循環(huán)發(fā)生嵌套關(guān)系時(shí)
外部循環(huán)i執(zhí)行1輪,內(nèi)部循環(huán)j完整地執(zhí)行了5輪
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 5; j++) {
System.out.println(i+"\t"+j);
}
}
2.4.4 死循環(huán)
當(dāng)循環(huán)無法自動執(zhí)行完畢,無限執(zhí)行時(shí),程序就是死循環(huán)
while(true){
}
上述代碼之后,編寫任何其他代碼都會報(bào)錯
綜合題目
模擬注冊登錄幸運(yùn)抽獎全過程
主要功能
1.注冊 2.登錄 3.幸運(yùn)抽獎
實(shí)現(xiàn)菜單的輸出顯示

實(shí)現(xiàn)循環(huán)執(zhí)行功能

實(shí)現(xiàn)注冊功能
需求說明
輸入用戶名和密碼,系統(tǒng)產(chǎn)生4位隨機(jī)數(shù)作為卡號。
注冊成功,顯示注冊信息并修改注冊標(biāo)識為true

實(shí)現(xiàn)登錄功能
需求說明
輸入注冊時(shí)的用戶名和密碼,登錄成功,系統(tǒng)提示歡迎信息
如果用戶名和密碼輸入錯誤,提示用戶繼續(xù)輸入,最多有3次輸入機(jī)會
