流程控制和控制语句入门

“流程控制”:就是指我们要安排我们的程序在一些正常情况下,或一些非正常情况下怎样去运行。这就需要做一些判断,并且基于这个判断,做一些“其它的事情”,这就是“流程控
制”。

判断语句_if语句:

if语句_格式1:
if(布尔表达式){
//如果"布尔表达式"的结果为true,则执行这里
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if (age >= 23 && age <= 25) {
System.out.println("年龄合适,那么你的身高呢?");
double h = sc.nextDouble();
if (h > 1.6) {
System.out.println("身高也很合适,那么你的月收入呢?");
int s = sc.nextInt();
if (s > 20000) {
System.out.println("年龄、身高、月薪都很合适...我们约会吧!!");
} } }
System.out.println("程序结束!");
}

if语句_格式二:
if(布尔表达式){
//如果布尔表达式返回true则执行这里
}else{
//如果布尔表达式返回false则执行这里
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你考试的分数:");
int s = sc.nextInt();
if (s >= 60) {
System.out.println("及格!");
}else{
System.out.println("不及格!");
}
System.out.println("程序结束!");
}

if语句_格式3:
if(布尔表达式1){
表达式1为true:执行这里
}else if(布尔表达式2){//表达式1为false:执行这里
表达式2为true:执行这里
}else if(布尔表达式3){//表达式2为false:执行这里
表达式3为true:执行这里
}else{//表达式3为fals e:执行这里
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的分数:");
int s = sc.nextInt();
if (s >= 0 && s < 60) {
System.out.println("凉凉!!");
} else if (s >= 60 && s < 80) {
System.out.println("及格!");
} else if (s >= 80 && s < 90) {
System.out.println("良好!");
} else if (s >= 90 && s <= 100) {
System.out.println("优秀!");
}else{
System.out.println("你的分数不正确!");
} } 

选择语句_switch语句:

1).switch语句同if语句的作用一样的,就是用于“判断”的。但在对多个值的等性判断时,使用switch语句结构更加清晰。
2).例如:用户输入1–7的值,程序要判断,并打印:星期一,星期二…

public class Demo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入1--7的星期值:");
int w = sc.nextInt();
//使用switch语句--switch(byte,short,int,char,String,枚举 变量/表达式)
switch (w){
case 1://case 后面必须是"常量",不能是”变量"/"表达式" System.out.println("星期一");
break;//表示:跳出switch语句
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4 :
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default://相当于:else{...}
System.out.println("你的输入有误!");
break; } } }

注意:
多个case的值不能有相同的;
每个case中的break表示:跳出switch语句,它不是必须的。但执行时,如果执行完case中代码,会无条件的继续执行后续case中的代码,直到遇到break;或者switch语句结束。
**

循环语句_for语句:

**
“循环”:反复的做一件事情。
例如:程序要打印10次HelloWorld

public static void main(String[] args) {
//打印10次HelloWorld
for(int i = 0 ; i < 10 ; i++){
System.out.println("HelloWorld!");
} }

循环语句_while循环语句:

格式: while(布尔表达式){
//如果"布尔表达式"为true,则执行循环体
//执行完毕,会向上,继续执行"布尔表达式"
}

public class Demo08 {
public static void main(String[] args) {
//1.打印10次HelloWorld
/*int i = 0;
while (i < 10) {
System.out.println("HelloWorld");
i++;
}*/

循环语句_do_while循环语句:

格式: do{
//循环体
}while(布尔表达式);
示例:

public class Demo {
public static void main(String[] args) {
//1.打印10次HelloWorld
/* int i = 0;
do{
System.out.println("HelloWorld");
i++;
}while(i < 10);*/

插播小知识变量的作用域:

变量的作用域范围就在定义它的大括号内,出了这个大括号,变量就不能被访问了。

public static void main(String[] args){
int a = 10;
if(...){
int b = 20;
System.out.println(a);//OK的
System.out.println(b);//OK的if(...){
int c = 30;
System.out.println(c);//OK的
System.out.println(a);//OK的
System.out.println(b);//OK的
}
System.out.println(c);//错误
}
System.out.println(a);//OK的
System.out.println(b);//错误
}

循环语句_死循环:

public static void main(String[] args) {
/*for(;; ) {
System.out.println("呵呵");
}*/
/*while (true) {
System.out.println("嘻嘻");
}
*/
do{
System.out.println("嘻嘻");
}while(true);
}

循环语句_嵌套循环:

1).嵌套循环:指一个循环中又包含了另一个循环;
例如:

public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5 ; j++) {
System.out.println("HelloWorld");
}
}
}

循环语句_continue和break语句:

1).continue : 结束本次循环,继续下一次循环;

//1.打印1--100所有的偶数
for(int i= 1 ;i <= 100 ;i++){
if(i % 2 == 0){
System.out.println(i);
} }

for(int i = 1 ;i <= 100; i++){
if(i % 2 != 0){
continue;//结束本次循环,继续下一次循环
}
System.out.println(i);
}

2).break:跳出循环语句(结束循环);

//1.从1开始,找出10个能同时被2,3,5整除的数
int i = 1;//存储数
int c = 0;//存储找到的个数
while(true){
if(i % 2 == 0 && i % 3 == 0 && i % 5 == 0){
c++;
System.out.println(i);
}
i++;
if(c == 10){
break;//跳出while循环
} }
发布了8 篇原创文章 · 获赞 0 · 访问量 38

猜你喜欢

转载自blog.csdn.net/FearSun/article/details/105381750
今日推荐