栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java流程控制;if选择;while循环;for循环6-7

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java流程控制;if选择;while循环;for循环6-7

顺序结构

Java最基本的结构就是顺序结构,除非特别指明,否则按顺序一句一句执行。

语句与语句之间,框与框之间按从上到下的顺序进行。

选择结构

if单选择结构

if双选择结构

if多选择结构

嵌套的if结构

switch多选择结构

if单选择
if (布尔表达式){
    //如果布尔表达式为true将执行的语句
}
package com.yy.struct;
//if单选择结构
import java.util.Scanner;
​
public class ifDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
​
        System.out.println("请输入内容:");
        String s = scanner.nextLine();
​
        //equals:判断字符串是否相等
        if (s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("End");
​
        scanner.close();
    }
}

if双选择结构
if (布尔表达式){    
    //如果布尔表达式为true
}else{    
    //如果布尔表达式为false
}
package com.yy.struct;
//if双选择
import java.util.Scanner;
​
public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数>60,及格
        Scanner scanner = new Scanner(System.in);
​
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
​
        if (score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
​
        scanner.close();
    }
}

if多选择结构
if (布尔表达式1){    
    //如果布尔表达式1为true
}else if(布尔表达式2){
    //如果布尔表达式2为true
}else if(布尔表达式3){
    //如果布尔表达式3为true
}else{    
    //如果布尔表达式都不为true执行代码
}
package com.yy.struct;
​
import java.util.Scanner;
​
public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
​
        if (score==100){
            System.out.println("恭喜满分!");
        }else if (score<100 && score>=90){
            System.out.println("A级");
        }else if (score<90 && score>=80){
            System.out.println("B级");
        }else if (score<80 && score>=70){
            System.out.println("C级");
        }else if (score<70 && score>=60){
            System.out.println("D级");
        }else if (score<60 && score>=0){
            System.out.println("成绩不及格!");
        }else {
            System.out.println("成绩不合法");
        }
​
        scanner.close();
    }
}

嵌套的if结构
if (布尔表达式1){    
    //如果布尔表达式1的值为true
    if (布尔表达式2){    
    //如果布尔表达式2的值为true
    }
}
switch多选择结构
switch(expression){
    case value:
        //语句
        break;//可选
    case value:
        //语句
        break;//可选
    default:
}

javaSE7开始

switch支持字符串String类型

package com.yy.struct;
​
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "yy";
        //jdk7的新特性,表达式结果可以是字符串!
        
        //反编译 java---class (字节码文件)----反编译(IDEA)
        switch (name){
            case "yy":
                System.out.println("yy");
                break;
            case "yushu":
                System.out.println("yushu");
                break;
            default:
                System.out.println("bye");
        }
    }
}

循环结构

while循环

do...while循环

for循环


while循环
while(布尔表达式){
    //循环内容
}
package com.yy.struct;
​
public class WhileDemo01 {
    public static void main(String[] args) {
​
        //输出1~100
        int i = 0;
​
        while (i < 100) {
            i++;
            System.out.println(i);
        }
​
    }
}
package com.yy.struct;
​
public class WhileDemo02 {
    public static void main(String[] args) {
        //计算1+2+3+...+100
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum+i;
            i++;
        }
​
        System.out.println(sum);
    }
}

do...while循环

do...while循环至少会执行一次

while先判断后执行,do...while先执行后判断

do{
    //代码语句
}while(布尔表达式);
package com.yy.struct;
//while+dowhile
public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("=======================");
        do {
            System.out.println(a);
            a++;
        }while (a<0);
    }
}

for循环
for(初始化;布尔表达式;更新){
    //代码语句
}
package com.yy.struct;
​
public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;  //初始化条件
​
        while (a<=100){//条件判断
            System.out.println(a);  //循环体
            a+=2;   //迭代
        }
​
        System.out.println("while结束");
​
        for(int i=1;i<=100;i++){//for(初始化;布尔表达式;更新)
            System.out.println(i);
        }
        System.out.println("for循环结束");
    }
}

for循环语句是最有效、最灵活的循环结构

for循环:最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。

package com.yy.struct;
​
public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算0-100之间的奇数和偶数的和
        int oddSum = 0;
        int evenSum = 0;
​
        for (int i = 0; i <= 100; i++) {
            if (i%2 != 0){//奇数
                oddSum+=i;
            }else {//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}

package com.yy.struct;
​
public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
​
        for (int i = 0; i < 1000; i++) {
            if (i%5 == 0){
                System.out.print(i+"t");
            }
            if (i%(5*3) == 0){
                System.out.println();
                //System.out.print("n");
            }
        }
        
    }
}

9*9乘法表自写:

package com.yy.struct;
​
public class ForDemo04 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int s = 1;s <= 9;s++)
            if (s<=i){
                int o = i*s;
                System.out.print(i+"*"+s+"="+o+"t");
            }else {
                System.out.println();
            }
        }
    }
}//问题:每行间隔不对

优化:

package com.yy.struct;
​
public class ForDemo05 {
    public static void main(String[] args) {
        
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j + "*" + i + "=" + (j*i) + "t");
            }
            System.out.println();
        }
    }
}
​

增强For循环
for (声明语句:表达式){
    //代码
}

增强for循环:主要用于数组或集合

package com.yy.struct;
​
public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组
​
        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        System.out.println("==============");
​
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}


break 、continue

break:用于强行退出循环,不执行循环中剩余的语句;

package com.yy.struct;
​
public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
            if (i == 30){
                break;  //到30结束循环
            }
        }
    }
}

continue:用于终止某次循环过程,即跳出循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

package com.yy.struct;
​
public class Continue {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100){
            i++;
            if (i%10 == 0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
        
​
    }
}

关于goto关键字:带标签的break和continue。(不用掌握)

package com.yy.struct;
​
public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        //质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
        int count = 0;
        
        //不建议使用
        outer:for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i/2; j++) {
                if (i % j == 0) {
                    continue outer; //outer:标签
                }
            }
            System.out.print(i + " ");
        }
    }
}
练习
package com.yy.struct;
​
public class TestDemo {
    public static void main(String[] args) {
        //打印三角形 5行
        for (int i = 1;i <= 5;i++){
            for (int j = 5;j >= i;j--){
                System.out.print(" ");
            }
            for (int j = 1;j <= i;j++){
                System.out.print("*");
            }
            for (int j = 1;j < i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号