1 流程控制语句
是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行,程序中大多数的代码都是这样执行的
1.2分支语句
1.2.1 if语句格式1及执行流程
根据情况判断,让程序执行不同的子模块。
if : 结构 如果怎么样就怎么样。
if(表达式){
执行语句;
}
当表达式的结果为true时,执行执行语句,否则不执行。
tips:表达式的结果类型必须是boolean类型。
public class Demo01 {
public static void main(String[] args) {
System.out.println("程序运行开始......");
int x = 8;
if(x > 9) {
System.out.println("X是真的大!");
}
System.out.println("程序运行结束......");
}
}
知识兔
1.2.2 if语句格式2及执行流程
if else 如果怎么样就怎么样,不然就怎么样
if(表达式){
if语句;
}else{
else语句;
}
世界上最远的距离就是你在if中,我在else中。
public static void main(String[] args) {
System.out.println("程序运行开始......");
int x = 8;
if(x > 9) {
System.out.println("X是真的大!");
}else {
System.out.println("X不是特别大!");
}
System.out.println("程序运行结束......");
}
知识兔tips:if和else语句永远都不可能同时执行。 也不可能都不执行。
1.2.3 if语句格式3及执行流程
if else if ... else 多种情况的判断分之
if(表达式1){
语句1;
} else if(表达式2){
语句2;
}。。。。
[else{语句}]
public static void main(String[] args) {
int mingci = 2;
if(mingci == 1) {
System.out.println("奖励一万块");
}else if(mingci == 2){
System.out.println("奖励五块");
}else if(mingci == 3) {
System.out.println("啥也没有!");
}else {
System.out.println("五年高考,三年模拟");
}
}
知识兔tips:只要一个条件判断为true,其余的条件就不会再判断了。
1.2.4 if语句练习
简单练习
package com.igeek.ex;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午10:05:51
*/
public class Ex1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个年份, 判断是闰年还是平年:");
int year = sc.nextInt();
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
System.out.println(year + "是闰年!");
} else {
System.out.println(year + "是平年");
}
}
}
知识兔成绩等级
package com.igeek.ex;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午10:09:36
*/
public class Ex2 {
public static void main(String[] args) {
int score = 95;
if (score >= 80 && score < 90) {
System.out.println("加班补课");
} else if (score >= 90) {
System.out.println("放假");
} else if (score >= 70 && score < 80) {
System.out.println("不要睡觉了");
} else {
System.out.println("别学了");
}
}
}
知识兔1.2.5 switch语句的格式及执行流程
switch(表达式){
case 表达1:
语句1;
break;
case 表达2:
语句2;
break;
.......
defaule:
语句
}
package com.igeek.demo;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午10:16:47
*/
public class Demo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的年级:");
int grade = sc.nextInt();
switch(grade) {
case 1:
System.out.println("学妹你好!我是XXXX");
break;
case 2:
System.out.println("兄弟!你好");
break;
case 3:
System.out.println("学姐!xxxx");
break;
case 4:
System.out.println("前辈!!");
break;
default:
System.out.println("社会人!");
}
}
}
知识兔switch不能做范围判断,只能做等值判断。
多重的if else也可以做等值判断。所以switch可以做的任务,多重的ifelse肯定可以做。但是多重的ifelse可以完成的任务switch不一定能完成。
当我们做等值判断时,要尽量使用switch,因为它效率高。
switch(表达式)
表达式的类型:在JDK1.7之前只能使用char和int类型。 在JDK1.7之后还可以使用String类型。 还有枚举类型。
switch中的break
没有break不影响程序编译。
注释掉上面的程序中的break;
break在这里的作用就是从switch中跳出,不在继续执行下面的程序。
switch中如果一个条件成立,则后面的条件就不会再判断的。 虽然不判断,但是它会直接执行后面的语句块中的程序。 所以break的作用是当执行了必要的程序之后,手动跳出break,不再执行后面不必要的程序。
1.2.6 switch语句练习
根据输入的时间,输出问候语句!
package com.igeek.ex;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午10:30:53
*/
public class Ex3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个时间:");
String str = sc.next();
switch(str){
case "早上":
System.out.println("奥哈药!");
break;
case "上午":
case "下午":
System.out.println("空你七万");
break;
case "晚上":
System.out.println("空八万");
break;
default:
System.out.println("亚麻带!");
break;
}
}
}
知识兔1.3循环语句结构
当需要重复执行相同或者相似的程序是,就可以使用循环语句
1.3.1 for循环的格式及基本使用
for(表达式1;表达式2;表达式3){
循环语句;
}
可以通过编译的语法:
for(;;){}
表达式1:当循环刚开始时执行一次,以后不会再执行。 表达式1中可以写任何要执行的语句。比如:定义语句,输出语句,方法调用等等。
表达式2:该表达式的结果类型必须是boolean值,每次循环语句执行之前会先判断表达式二的结果,如果是true则执行循环语句,如果是false,则不执行循环语句。
表达式3:每次循环结束之后执行一次。 表达式3中可以写任何要执行的语句。
案例:
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午10:40:54
*/
public class Demo5 {
public static void main(String[] args) {
int i = 0;
for(System.out.println("表达式1");i < 3;System.out.println("表达式3")) {
System.out.println(i+"、我要暴富!");
i ++;
}
}
}
知识兔执行结果:
案例:输出1~5
public static void main(String[] args) {
//在控制台循环的输出1~5
for(int i = 0;i < 5;i++) {
System.out.println(i+1);
}
}
知识兔tips:如何时候,如果希望循环执行nci次,则让i=0;条件为i<n;
for循环一般都是用来执行已经知道循环次数的循环。
tips:
public static void main(String[] args) {
//在控制台循环的输出1~5
for(int i = 0,x = 10;i < 5 && x > 8;i++,x--) {
System.out.println(i+1);
}
}
知识兔在定义语句中可以同时定义多个变量,在表达式3中也可以同时写多个语句。表达式2中可以是组合条件。
1.3.2 while循环的结构和使用
while(表达式){
循环语句;
}
表达式必须是boolean类型的表达式。
表达式的值为true时,执行循环语句。否则退出循环。
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:14:55
*/
public class Demo7 {
public static void main(String[] args) {
int i = 0;
while(i<5) {
System.out.println(i+"、武汉贼热!");
i++;
}
}
}
知识兔tips:while循环一般是用来循环不知道循环次数的循环。比如死循环。
案例:输入学生的成绩,计算总分,输入-1表示结束。
package com.igeek.demo;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:18:25
*/
public class Demo8 {
//输入学生的成绩,计算总分,输入-1表示结束。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = 0;
int count = 1;
int sum = 0;
while(score != -1) {//判断成绩不是-1就继续循环。
System.out.println("请输入第"+count+"位同学的成绩:");
//每次在循环的内部输入一个成绩
score = sc.nextInt();
sum += score;// sum = sum + score;
count++;
}
System.out.println(sum);
}
}
知识兔1.3.3 do while循环的结构和使用
do{
循环语句;
}while(表达式);
先执行循环语句,判断表达式,如果表达式的值为true则执行循环语句。否则退出循环。
和while的不同就是先执行语句,再判断。
do while在任何情况下都会执行一次。
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:26:30
*/
public class Demo9 {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i + "、还是要暴富!");
i++;
} while (i < 5);
}
}
知识兔do while一般也是用来执行不知道循环次数的循环。
1.3.4 三种循环的总结和区别
for:一般都是用来执行知道循环次数的循环。
while和dowhile一般都是用来执行不知道循环次数的循环。
for和while都是先判断执行循环。
dowhile是先循环在判断。
所以for和while有可能一次都不执行,而dowhile一定至少会执行一次。
各种循环其实都可以相互代替。
1.4循环控制语句
1.4.1 break语句
可以跳出循环,中断循环。
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:34:47
*/
public class Demo10 {
public static void main(String[] args) {
System.out.println("程序开始执行......");
// 输出7的倍数,如果数字大于50就停止输出
for (int i = 0; true; i++) {
if(i>50) {
break;//退出循环,执行循环外的程序
}
if (i % 7 == 0) {
System.out.println(i);
}
}
System.out.println("程序执行结束.......");
}
}
知识兔修改上面计算总分的案例
package com.igeek.demo;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:18:25
*/
public class Demo11 {
//输入学生的成绩,计算总分,输入-1表示结束。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int count = 1;
do {
System.out.println("请输入第"+count+"为同学的成绩:");
int score = sc.nextInt();
if(score==-1) {
break;//如果输入的成绩是-1就退出循环
}
sum += score;
count ++ ;
}while(true);
System.out.println("总分是:"+sum);
}
}
知识兔案例:
输入两个数,计算两个数的最小公倍数。
package com.igeek.demo;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:40:59
*/
public class Demo12 {
// 输入两个数,计算两个数的最小公倍数。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int n1 = sc.nextInt();
System.out.println("请输入第二个数:");
int n2 = sc.nextInt();
// 找出比较大的数
int max = n1 > n2 ? n1 : n2;
// 从n2开始往上循环
while (true) {
if (max % n1 == 0 && max % n2 == 0) {
System.out.println(n1+"和"+n2+"的最小公倍数是:"+max);
break;
}
max ++ ;
}
}
}
知识兔1.4.2 continue语句
continue意思是结束本次循环,继续下次循环。
案例:输出1~100所有的偶数
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日上午11:47:03
*/
public class Demo13 {
public static void main(String[] args) {
// :输出1~100所有的偶数
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
continue;//结束本次循环,继续下次循环。
}
System.out.println(i);
}
}
}
知识兔1.5 变量的作用域
什么是变量的作用域?
什么是作用域?
作用域就是一个变量从申明开始到结束的一个范围。生命周期。
一个变量从申明开始到离他最近的包裹它的结束的花括号。
package com.igeek.demo;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日下午2:40:23
*/
public class Demo14 {
int x = 1000;// 成员变量
public static void main(String[] args) {
int x = 10;
System.out.println(x);// 10
int y = x + 10;
if (1 == 1) {
//int x = 1000;
int z = x * 10;
System.out.println(++x);// 11
System.out.println(z);// 100
}
// System.out.println(z);
//for循环的表达式中定义的变量作用于就是一个循环中
for (int i = 0; i < 5; i++) {
int j = i+10;//j的作用域就是一次循环过程中
System.out.println(i);
System.out.println(j);
}
for (int i = 0; i < 5; i++) {
}
}
}
知识兔1.6 几个循环的练习
①计算1~100之间所有可以被3整除的数的和
/**
* @param args
*/
// 计算1~100之间所有可以被3整除的数的和
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
sum += i;// sum = sum + i;
}
}
System.out.println(sum);
}
知识兔②水仙花数
153 = 1*1*1+5*5*5+3*3*3
编程找出100~1000之间的所有的水仙花数。
package com.igeek.ex;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日下午2:59:38
*/
public class Ex5 {
/**
* 153 = 1*1*1+5*5*5+3*3*3
*
* 编程找出100~1000之间的所有的水仙花数。
*/
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
// 取出i的每个位上的数字,并且计算立方和
int g = i % 10;
int s = i / 10 % 10;
int b = i / 100;
if (i == g * g * g + s * s * s + b * b * b) {
System.out.println(i);
}
}
}
}
知识兔1.7控制台的万年历
已知1900年1月1日是星期一。请输出本月的日历。
效果:
请输入年份:
1985
请输入月份:
10
1985年10月日历如下:
package com.igeek.ex;
import java.util.Scanner;
/**
* @company 武汉极客营
*
* @author www.igeekhome.com
*
* 2019年8月14日下午3:07:27
*/
public class Calendar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();// 2019
System.out.println("请输入月份:");
int month = sc.nextInt();// 8
// 根据输入的年份和月份,计算当年当月的第一天是周几,当年当月的总天数是多少
// 当月第一天是周几
// 计算1900-1-1到当月之前的总天数
int total = 0;
for (int y = 1900; y < year; y++) {
}
for (int m = 1; m < month; m++) {
}
int firstDay = (total % 7) + 1;
// 如果第一天是周一,则修改为0
firstDay = firstDay == 7 ? 0 : firstDay;
// 计算当月的总天数
// 当月的总天数
int days = 31;
switch (month) {
}
// 循环的总次数
int count = days + firstDay;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < count; i++) {
}
}
}
知识兔