JAVA学习——流程控制

铭记

顺序结构是核心!

if选择

package com.liang.struct;
import java.util.Scanner;
public class IfDemo02 {
    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("恭喜你!本次考试:优。");
        }
        else if (score<90&&score>=80){
            System.out.println("恭喜你!本次考试:良");
        }
        else if (score<70&&score>=60){
            System.out.println("恭喜你!本次考试:及格");
        }
        else if (score<60){
            System.out.println("很遗憾,本次考试落榜了。");
        }
        else{
            System.out.println("该成绩超出统计范围!");
        }
    }
}

结果示例:

/*请输入成绩:
	100
	恭喜你!本次考试满分!
	
	请输入成绩:
	120
	该成绩超出统计范围!
	
	请输入成绩:
	50
	很遗憾,本次考试落榜了。*/

Switch

package com.liang.struct;
import java.util.Scanner;
public class SwitchDemo01 {
    public static void main(String[] args) {
        //case的穿透现象
        //switc匹配一个具体的值
        System.out.println("请输入暗号:");
        Scanner scanner=new Scanner(System.in);
        String names=scanner.nextLine();
        //JDK7后,表达式结果可以为字符串
        switch (names){
            case "大神":
                System.out.println("错误。");
                break;//可选
            case "小白":
                System.out.println("没错!菜鸟");
                break;//可选
            default:
                System.out.println("你说的是啥???");
                break;//可选
        }
    }
}

结果示例:

/*请输入暗号:
	大神
	错误。
	
请输入暗号:
	小白
	没错!菜鸟

请输入暗号:
	彭于晏
	你说的是啥???*/

*While循环

//练习
package com.liang.struct;
//用while循环计算1--1000之间能被5整除的数,每行输出3个
public class WhileDemo03 {
    public static void main(String[] args) {
        int i=0;//初始化值
        while(i<=1000){//条件判断
            i++;//迭代(更新),设置终止循环的一步
            if(i%5==0){
                System.out.print(i+"\t");// \t 制表符
            }
            if (i%(5*3)==0){
                System.out.println();
            }
        }
    }
}

Do While循环

package com.liang.struct;
public class DoWhileDemo01 {
    public static void main(String[] args) {
        //while和do while的区别
        int a=1;
        while (a<5){//先判断,再执行
            a++;
            System.out.println(a);
        }
        System.out.println("============");
        do{
            System.out.println(a);
            a++;//先执行
        }while(a<10);//再判断
    }
}

*for循环

package com.liang.struct;
//练习:用for循环计算1-100之间能被5整除的数,并且每行输出3个
public class ForDemo02 {
    public static void main(String[] args) {
        //初始值,条件判断,迭代
        for (int i = 0; i <= 100; i++) {
            if (i%5==0){//能被5整除
                System.out.print(i+"\t");//输出并横向跳到下一制表符度位置
            }
            if (i%(5*3)==0){//当输出的数字能整除15时,换行
                System.out.println();
                //System.out.print("\n");两种方法都可以
            }
            //println:输出并换行
            //print:输出后不换行
        }
    }
}

经典练习——打印九九乘法表

package com.liang.struct;
/*
1*1=1
1*2=2	2*2=4
1*3=3	2*3=6	3*3=9
1*4=4	2*4=8	3*4=12	4*4=16
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81
 */
public class ForDemo03 {
    public static void main(String[] args) {
        //1.先打印一列
        //2.将固定的1再用循环包起来
        //3.去掉重复项 j<=i
        //调整九九乘法表的样式
        for (int i = 1; i <= 9; i++) {
            for (int j = 1;j <= i; j++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");//没有ln不换行
            }
            System.out.println();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值