第一部分:跟着PPT走去看一下流程控制的语句执行规则:
了解规则后去观察下面的代码:运行结果不展示:流程控制的语句语法要自己去领悟的。
七大语句循环
单分支结构: if(boolean类型的条件表达式/数值/boolean类型变量){ 语句块; } 执行的规则: 如果if小括号的值为true,程序执行大括号里面的语句块,否则,将结束整个if的分支结构,程序执行if这个结构后面的语句
双分支结构: if(boolean类型的条件表达式/数值/boolean类型变量){ 语句块1; }else{ 语句块2; }
多分支结构: if(boolean类型的条件表达式/数值/boolean类型变量){ 语句块1; }else if(boolean类型的条件表达式/数值/boolean类型变量){ 语句块2; }else if(boolean类型的条件表达式/数值/boolean类型变量){ 语句块3; }else{ 语句块4; }
执行规则:当程序看到if多分支结构时,首先进入if小括号boolean类型的条件表达式的判断,如果为true,程序会执行第一个大括号里面语句块1的所有内容,执行完后会 结束整个多分支 结构,如果为 false,程序会执行第一个else if小括号boolean类型的条件表达式的判断,如果为TRUE,程序会执行第二个大括号里面语句块2的所有内容, 执行完后会结束整个多分支 结构,否则,程序会执行第二个else if小括号boolean类型的条件表达式的判断,如果为TRUE,程序会执行第三个大括号里面语句块3的所有内容, 执行完后会结束整个多分支 结构,否则程序会执行else后面的这个大括号语句块4的所有的内容。
switch结构: switch(数值类型的表达式/数值){ case 数值1:一系列语句1; case 数值2:一系列语句2; case 数值3:一系列语句3; ........... case n:一系列语句n; default: 一系列语句; }
执行规则: 当程序看到switch结构时,会用小括号的数值与case后面的数值进行一一的比较,比较是否相等,如果相等,将会执行case后面的所有的一系列语句并且包括 default后面的一系列语句,如果都没有匹配上,那么程序会执行default后面的一系列语句
循环4要素: 1.循环的起点 2.循环要做的事情 3.循环起点到终点的变化 4.循环的终点
while循环: while(循环条件的判断){循环体} 执行规则:
当程序看到while循环是,首先进入循环条件的判断,如果循环条件为TRUE,那么程序进入循环体,执行循环体内所有的语句,直到看到循环的右大括号时, 程序再一次进入循环条件的判断,依次执行,直到循环条件为假的时候,整个while循环结束。
package com.Java.Basics1; public class as { public static void main(String[] args) { System.out.println("今天是你的回顾期第一天,走进java程序员"); System.out.println("java程序第一课"); System.out.println("思考一下3*5用java语言用什么输出呢"); int a=3; int b=5; int c=a*b; System.out.println("3*5=15"+a*b); System.out.println(c); System.out.println("================"); byte aa=123; short ba=32564; int ca=4578662; long da=467896123; long ea=aa+ba+ca+da; System.out.println(aa); System.out.println(ba); System.out.println(ca); System.out.println(da); System.out.println(ea); System.out.println(aa/8); System.out.println(ba*8); System.out.println(ca+45692); System.out.println(da-45623); System.out.println(ea^45); int aaa=13; if(aaa>12) { System.out.println("I IOVER YOU"); } } }
package com.Java.Basics1; public class ComB { public static void main(String[] args) { // TODO Auto-generated method stub int number1=6; int number2=7; int number3=34; int c=2; int v=5; boolean result=((c>v)&&(c!=v)); boolean result1=((c>v)||(c!=v)); System.out.println(result); System.out.println(result1); System.out.println((number1>number2)); System.out.println("===1=="); System.out.println((number1<number2)); System.out.println("===2=="); System.out.println((number1==number3)); System.out.println("===3=="); System.out.println((number1!=number2)); System.out.println("==4==="); System.out.println((number1>=number2)); System.out.println("===5=="); System.out.println((number1<=number2)); } }
package com.Java.Basics1; public class ComD { public static void main(String[] args) { // TODO Auto-generated method stub /* * if else 语句 */ int math =102; int english=120; if(math>110) { System.out.println("66666"); }else { System.out.println("les go"); } if(english<110) { System.out.println("66666"); }else { System.out.println("les go"); } /* * if()else....if()else 多分支 */ int x=20; if(x>30) { System.out.println("a>30"); }else if(x>20) { System.out.println("a>20 ||a<10"); }else if (x>0) { System.out.println("a<0"); }else { System.out.println("a>>0"); } /* * switch case break default 语句 一 */ String str="今朝有酒1"; switch(str) { case "今": System.out.println("7"); break; case"朝": System.out.println("4"); break; case"有": System.out.println("3"); break; case"今朝有酒": System.out.println("今朝有酒 今朝有酒"); break; default: System.out.println("i donot know"); } /* * switch case break default */ int week=7; switch (week) { case 0: System.out.println("1"); break; case 1: System.out.println("1"); break; case 2: System.out.println("1"); break; case 3: System.out.println("1"); break; case 4: System.out.println("1"); break; case 5: System.out.println("1"); break; case 6: System.out.println("1"); break; case 7: System.out.println("sunday"); break; default: System.out.println("0k not see you"); } /* * while() */ int y=1; int sum=0; while (y<=10) { sum=sum+y; y++; } System.out.println("1到10的和"+sum); } }
package com.Java.Basics1; public class ComD { public static void main(String[] args) { // TODO Auto-generated method stub /* * if else 语句 */ int math =102; int english=120; if(math>110) { System.out.println("66666"); }else { System.out.println("les go"); } if(english<110) { System.out.println("66666"); }else { System.out.println("les go"); } /* * if()else....if()else 多分支 */ int x=20; if(x>30) { System.out.println("a>30"); }else if(x>20) { System.out.println("a>20 ||a<10"); }else if (x>0) { System.out.println("a<0"); }else { System.out.println("a>>0"); } /* * switch case break default 语句 一 */ String str="今朝有酒1"; switch(str) { case "今": System.out.println("7"); break; case"朝": System.out.println("4"); break; case"有": System.out.println("3"); break; case"今朝有酒": System.out.println("今朝有酒 今朝有酒"); break; default: System.out.println("i donot know"); } /* * switch case break default */ int week=7; switch (week) { case 0: System.out.println("1"); break; case 1: System.out.println("1"); break; case 2: System.out.println("1"); break; case 3: System.out.println("1"); break; case 4: System.out.println("1"); break; case 5: System.out.println("1"); break; case 6: System.out.println("1"); break; case 7: System.out.println("sunday"); break; default: System.out.println("0k not see you"); } /* * while() */ int y=1; int sum=0; while (y<=10) { sum=sum+y; y++; } System.out.println("1到10的和"+sum); } }
package com.Java.Basics1; public class ComF { public static void main(String[] args) { /* * for语句 */ int sum=0; for (int i = 0; i < 6; i++) { sum=sum+i; System.out.println("hellow world"); } System.out.println(sum); System.out.println("-=========="); for (int g = 0; g < 3; g++) { for (int h = 0; h < 6; h++) { if(h==4) { break; } System.out.println("g="+g+h + "h="+g); } } for (int k = 0; k <28; k++) { if(k%2==0) { continue; } System.out.println(k+" "+"k%2≠0"); } } }
package com.eng; public class Eng4 { public static void main (String[]args) { //1 int age=20; boolean result=age>=18?(true):(false); System.out.println(age); //2 class masx{ { int g =2; System.out.println(g); } { int g =2; System.out.println(g); } { int g =2; System.out.println(g); } { int g =2; System.out.println(g); } } } }
package com.eng; import java.util.Scanner; public class Eng6 { public static void main (String[]args) { Scanner sc = new Scanner (System.in); System.out.print("请输入你的消费金额:"); double sMoney = sc.nextDouble(); if (sMoney<=0) { System.out.println("你输入有!!!!!"); }else if(sMoney<50) { System.out.println("换购活动需要50元,谢谢你的参加"); }else if(sMoney>=50){ System.out.println("1 满50元, 加2瓶可乐"); System.out.println("2 满100元 ,加3瓶酸奶"); System.out.println("3 满100元,有10元面粉"); System.out.println("4 满200元,30元的喝喝"); System.out.println("5 满100元, 加5本子"); System.out.println("6 满300元, 加15个本子算20元"); System.out.println("0 不换"); System.out.println("请输入你的序号"); int a =sc.nextInt(); if(a>5 ||a<0) { System.out.print("你输入有误!如有需要轻重开始"); }else { if(a==6 && sMoney>=200) { System.out.println("满300元, 加15本子"); System.out.println("本次消费总额为:"+(sMoney+40)); }else if(a==5 && sMoney>=100) { System.out.println(" 满100元,90元的喝喝"); System.out.println("本次消费总额为:"+(sMoney+45)); }else if(a==4 && sMoney>=200) { System.out.println(" 满200元,30元的喝喝"); System.out.println("本次消费总额为:"+(sMoney+30)); }else if(a==3 && sMoney>=100) { System.out.println(" 满100元,有10元面粉"); System.out.println("本次消费总额为:"+(sMoney+10)); }else if(a==2 && sMoney>=100) { System.out.println(" 满100元 ,加3瓶酸奶"); System.out.println("本次消费总额为:"+(sMoney+19)); }else if(a==1 && sMoney>=50) { System.out.println(" 满50元, 加2瓶可乐"); System.out.println("本次消费总额为:"+(sMoney+4)); }else if(a==0 ) { System.out.println("欢迎光临!!!"); }else { System.out.println("你没有达到相应金额"); } } } } }
package com.eng; public class Eng7 { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=1;i<=9;++i){ for(int j=1;j<=i;++j){ System.out.print(i+"*"+j+"="+i*j+" "); } System.out.println(); } } }
package com.eng; public class Eng9 { public static void main(String[] args) { // TODO Auto-generated method stub //1 int i = 10; while(i>=0){ System.out.println(i); --i; } //2 for(int a =1;a<=10;a =a+1){ System.out.println(a); } //3 int a = 1; while (a<=1000){ if(a%2==0){ System.out.println(a); } a++; } //4 int f=1; do{ System.out.println(f); ++f; }while(f<=100); //5 int h =1; int j =1; do{ System.out.println(j); j =j+4; h++; }while(h<=50); //6 int t =1; int sum =0; do{ System.out.println(t); sum =sum+t; ++t; }while(t<=100); System.out.println(sum); } }
package com.eng; import java.util.Scanner; public class Eng10 { public static void main(String[] args) { //1.创建Scanner对象 Scanner scanner = new Scanner(System.in); //2.提示用户输入年份并且获取年份 System.out.println("请输入年份"); int year = scanner.nextInt(); //3.提示用户输入月份并且获取月份 System.out.println("请输入月份"); int month = scanner.nextInt(); //4.提示用户输入日期并且获取日期 System.out.println("请输入日期"); int day = scanner.nextInt(); //请输入时间 System.out.println("请输入时间"); int house =scanner.nextInt(); //5.定义一个变量去统计一共过了多少天 int sum = 0; switch(month){ case 12: if(month==12){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 11: if(month==11){ sum = sum+day; }else{//过满了整月 sum = sum+30; } case 10: if(month==10){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 9: if(month==9){ sum = sum+day; }else{//过满了整月 sum = sum+30; } case 8: if(month==8){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 7: if(month==7){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 6: if(month==6){ sum = sum+day; }else{//过满了整月 sum = sum+30; } case 5: if(month==5){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 4: if(month==4){ sum = sum+day; }else{//过满了整月 sum = sum+30; } case 3: if(month==3){ sum = sum+day; }else{//过满了整月 sum = sum+31; } case 2: if(month==1){ sum = sum+day; }else{//过满了整月并且要判断是闰年还是平年 if((year%400==0)||(year%4==0&&year%100!=0)){//闰年 sum = sum+29; }else{//平年 sum = sum+28; } } case 1: if(month==1){ sum = sum+day; }else{//过满了整月 sum = sum+31; } } int sum1= 0; switch(house){ case 1: if(house==1){ sum1 = sum1+house; }else{//过满了整月 sum1 = 1; } case 2: if(house==2){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 3: if(house==3){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 4: if(house==4){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 5: if(house==5){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 6: if(house==6){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 7: if(house==7){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 8: if(house==8){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 9: if(house==9){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 10: if(house==10){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 11: if(house==11){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 12: if(house==12){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 13: if(house==13){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 14: if(house==14){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 15: if(house==15){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 16: if(house==16){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 17: if(house==17){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 18: if(house==18){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 19: if(house==19){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 20: if(house==20){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 21: if(house==21){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 22: if(house==22){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 23: if(house==23){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } case 24: if(house==24){ sum1 = sum1+house; }else{//过满了整月 sum1 = sum1+1; } System.out.println(year+"年"+month+"月"+day+"日是该年的第"+sum+"天"+"小时的时间"+sum1); System.out.println("-------------------------------------------------"); System.out.println(year+"年"+month+"月"+day+"日是该年的第"+sum+"天"+"时间"+sum1); System.out.println(year+"年"+month+"月"+day+"日是该年的第"+sum+"天"+"小时的时间"+sum1); System.out.println("-------------------------------------------------"); System.out.println(year+"年"+month+"月"+day+"日是该年的第"+sum+"天"+"时间"+sum1); } } }