Java程序逻辑控制

前言

本节重点掌握Java程序中的逻辑控制语句,掌握Java的输入输出方式,实现才数字游戏。

一、顺序结构

顺序结构是Java中最简单、最基本、最常用的一种程序结构,执行时是按照书写顺序逐条执行

若将代码顺序换一下,执行的结果也是会发生变化的。 

二、选择结构(分支结构) 

1.if语句

语法格式

if(布尔表达式){

//语句

}

eg: 

if score=93;
if(score>=90){
System.out.println("吃一顿火锅!");
}

 if(布尔表达式){

//语句1

}else{

//语句2

}

eg: 

​
if score=93;
if(score>=90){
System.out.println("吃一顿火锅!");
}else{
System.out.println("喝西北风!");
}

​

if(布尔表达式1){

//语句1

} else if{

//语句2

}else{

//语句3

}

表达式1成立,执行语句1,否则表达式 2成立,执行语句2,否则执行语句3

eg:

if(score>=90){
System.out.println("成绩优秀");
}else if(score>=80&&score<90){
System.out.println("成绩良好");
}else if(score>=70&&score<80){
System.out.println("成绩中等");
}else if(score>=60&&score<70){
System.out.println("成绩及格");
}else{
System.out.println("成绩不及格");
}

代码展示

判断三个数的大小:

 int a,b,c;
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入三个数:");
        a=scan.nextInt();
        b=scan.nextInt();
        c=scan.nextInt();
        if(a<b){
            a=b;
        }
        if (a>c){
            c=a;
        }
        System.out.println("a="+a);
        System.out.println("c="+c);
    }

 2.switch语句

基本语句

case 常量值1{

语句1;

[break;]

}

case 常量值2:{

语句2;

[break;]

}

...

default{

内容都不满足时执行语句;

[break];

}

执行流程:
1. 先计算表达式的值
2. case 依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到 break 时结束
3. 当表达式的值没有与所列项匹配时,执行 default

判断一个年份是否为闰年:

public static void main(String[] args) {
        int year;
        int month;
        int days=0;
        System.out.println("请输入年月");
        Scanner sc=new Scanner(System.in);
        year=sc.nextInt();
        month= sc.nextInt();
        switch (month){
            case 2:
                if((year%4==0&&year%100!=0)||year%400==0){
                    days=29;
                }
                else{
                    days=28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            default:
                System.out.println("输入月份数据不正确!");

        }

        System.out.println(year+"年"+month+"月有"+days+"天");

    }
注意事项
  • 多个case后的常量值不可以重复
  • switch的括号内只能是以下类型的表达式:
  • 基本类型:bytecharshortint,注意不能是long类型
  • 引用类型:String常量串、枚举类型

三、循环结构 

1.while循环结构

语法格式:

while循环结构{

循环结构;

}

循环条件为 true, 则执行循环语句; 否则结束循环.

计算1—100的和

int n = 1;
int result = 0;
while (n <= 100) {
result += n;
n++;
}
System.out.println(num);

// 执行结果
5050

2.break

遇到break就会结束执行代码

3.continue

continue 的功能是跳过这次循环 , 立即进入下次循环 .
代码示例 : 找到 100 - 200 中所有 3 的倍数
int num = 100;
while (num <= 200) {
if (num % 3 != 0) {
num++; // 这里的 ++ 不要忘记! 否则会死循环.
continue;
}
System.out.println("找到了 3 的倍数, 为:" + num);
num++;
}

4.for循环

基本语法
表达式 1: 用于初始化循环变量初始值设置,在循环最开始时执行,且只执行一次
表达式 2: 循环条件,满则循环继续,否则循环结束
表达式 3: 循环变量更新方式
代码
  public static void main(String[] args) {
        int teg = 1;
        int i = 5;
        while (i >= 1) {
            teg *= i;
            i--;
        }
        System.out.println("5的阶乘是" + teg);
    }

四、输入输出

基本语法
System . out . println (abc ); // 输出一个字符串 , 带换行
System . out . print (abc ); // 输出一个字符串 , 不带换行
System . out . printf ( format , msg ); // 格式化输出
  • println 输出的内容自带 \n, print 不带 \n
  • printf 的格式化输出方式和 C 语言的 printf 是基本一致的.

键盘输入 

 int a,b,c;
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入三个数:");
        a=scan.nextInt();
        b=scan.nextInt();
        c=scan.nextInt();
      
        System.out.println("a="+a);
        System.out.println("b="+b);
        System.out.println("c="+c);
    }

五、猜数字游戏

public static void main(String[] args) {
      int ran=(int)(Math.random()*50+1); //生成随机数
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入一个1~50之间的数据:");
        for(;;) {
            int res = sc.nextInt();
            if (res != ran) {
                if (res > ran) {
                    System.out.println("输入的结果太大了");
                } else {
                    System.out.println("输入的结果太小了");
                }
                System.out.println("请重新输入一个数字");
            } else {
                System.out.println("恭喜你,猜对了!!!");
                break;
            }
        }
   }


以上就是本期所有的内容!请各位大佬指点。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值