Day04 Java基础2(流程控制)

运算符

1.2算数运算符

±*/

/除法是商,%是取余


1.3字符的+操作

拿字符在计算机底层的数据值来计算的

A-65 A-Z是连续的

a-97 a-z是连续的

0-48 0-9是连续的

编译按照每行实现,先定义了char,后来改了

int i=6;
char c='a';
c='0';//值为48
System.out.println(i+c);
//输出为54

1.4字符串的+操作

printin("max:"+x);//输出为max:50

拼接 与int等都能拼接且可以不注意顺序


2.1赋值运算符

s = (short) (s+20);//强转成short类型

3.1自增自减运算符

a++ ++a

最常见的做法是单独使用


4.1关系运算符

注意事项

关系运算符的结果都是boolean类型 true/false


5.1逻辑运算符


5.2短路逻辑运算符


6.1三元运算符

  • 格式:关系表达式 ?表达式1 :表达式2
  • a > b ? a : b;

NO.1计算关系表达式的值

如果true,结果为表达式1

反之flase,结果为表达式2

数据输入

输入数字比大小

import java.util.Scanner;//导包  导包动作必须在类定义的上面
public class Hello {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//创建对象(只有sc是变量名可以修改,其他不能变)
        System.out.println("输入第一个身高");
        int x1 = sc.nextInt();//接收数据(只有x1是变量名能变,其余不行)
        System.out.println("输入第二个身高");
        int x2 = sc.nextInt();
        System.out.println("输入第三个身高");
        int x3 = sc.nextInt();

        int x4= (x1 > x2) ? x1 : x2;
        int xmax=(x4>x3?x4:x3);
        System.out.println("身高最高的是:" + xmax +"cm");//输出数据

        //自己输入3个值然后对比

    }
}

分支语句

流程控制语句分类

  • 顺序结构(从上往下依次执行)
  • 分支结构(if,switch)
  • 循环结构(for,while,do…while)

if语句

if(关系表达式){
    语句;
}
public class demp02 {
    public static void main(String[] args) {
        int a= 10;
        int b= 20;
        if (a==b){
            System.out.println("nb");
        }
        System.out.println("sb");
    }
}

if语句格式2

public class demp02 {
    public static void main(String[] args) {
        int a= 10;
        int b= 20;
        int c= 15;
        if (a==b){
            System.out.println("nb");
        }else{
            System.out.println("nb2");
        }
        System.out.println("sb");
    }
}

判断奇偶

import java.util.Scanner;//导包
public class demp02 {
    public static void main(String[] args) {
        System.out.println("输入数字");
        Scanner sc = new Scanner(System.in);//创建对象
        int number = sc.nextInt();//接收数据
        if (number % 2 == 0){
            System.out.println(number+"是偶数");
        }else{
            System.out.println(number+"是奇数");
        }

    }
}
  • if语句格式3(考试奖励代码)

    import java.util.Scanner;
    public class demp02 {
        public static void main(String[] args) {
            System.out.println("输入小明的考试成绩");
            Scanner sc =  new Scanner(System.in);
            int score = sc.nextInt();
            if(score>100 || score<0){
                System.out.println("输入成绩有误");
            }else if (score >= 95 && score<=100){
                System.out.println("山地自行车");
            }else if(score < 95 & score>=90){
                System.out.println("游乐园");
            }else if(score < 90 & score>=80){
                System.out.println("变形金刚");
            }else {
                System.out.println("吊打一顿");
            }
        }
    }
    //数据测试:正确数据、边界数据、错误数据
    

switch语句

import java.util.Scanner;
public class demp02 {
    public static void main(String[] args) {
        System.out.println("输入月份");
        Scanner sc =  new Scanner(System.in);
        int month = sc.nextInt();
        switch (month){
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
                break;
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
                break;
            default:
                System.out.println("输入有误");
        }
    }
}
//利用case穿透完成精简的输入月份看季节

循环语句


for循环语句

结构组成:

初始化语句 :可以一条或多条

条件判断语句:使用结果为boolean类型的表达式,这个表达式能决定是否执行循环

循环体语句:循环语句

条件控制语句:通常一条语句,达到控制循环是否继续向下执行的效果,常见i++


格式:

for(初始化语句;条件判断语句;条件控制语句){
    循环体语句;
}
public class demo3 {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1;i<=5;i++){
            sum += i;
        }
        System.out.println("数据和为"+ sum);
    }
}
//1-5求和
public class demo3 {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1;i<=100;i++){
            if(i % 2 ==0){
                sum += i;
            }

        }
        System.out.println("偶数和为"+ sum);
    }
}
//0-100求偶数和
public class demo3 {
    public static void main(String[] args) {
        for(int i=100;i<=999;i++){
            int ge = i%10;
            int shi = i/10%10;
            int bai = i/10/10%10;
            if(ge*ge*ge + shi*shi*shi + bai*bai*bai==i){
                System.out.println("水仙花数为"+ i);
            }
        }
    }
}
//输出所有水仙花数
public class demo3 {
    public static void main(String[] args) {
        int count=0;
        for(int i=100;i<=999;i++){
            int ge = i%10;
            int shi = i/10%10;
            int bai = i/10/10%10;
            if(ge*ge*ge + shi*shi*shi + bai*bai*bai==i){
                count++;

            }

        }
        System.out.println("水仙花共有"+ count);
    }
}
//统计水仙花个数

while循环语句

初始化语句;
while(条件判断语句){
    循环体语句;
    条件控制语句;
}
public class demo4 {
    public static void main(String[] args) {
        int i=1;
        while (i<=5){
            System.out.println("nb");
            i++;

        }
    }
}
public class demo4 {
    public static void main(String[] args) {
        int count=0;
        double paper = 0.1;
        int zf = 8844430;
        while(paper <= zf){
            paper *= 2;
            count++;
        }
        System.out.println("折叠"+count+"次");
    }
}
//折叠珠峰

do …while循环

初始化语句;
do{
    循环体语句;
    条件控制语句;
}while(条件判断语句)

三种循环的区别

do…while会先执行一侧循环体(先执行后判断)

for和while循环是先进行条件判断在选择执行(先判断再执行)

for和while的区别

条件控制语句所控制的自增变量,在for循环中,for循环结束后,访问不到了

条件控制语句所控制的自增变量,不在while循环中,for循环结束后,访问不到了

死循环

for(;;){}
while(true){}
do{}while(true)
//doc窗口中ctrl+c结束死循环

跳转控制语句

contiune;//用在循环中,基于条件控制,跳过某次循环体内容的执行,继续下次的执行
break;//终止整个循环

循环嵌套

public class demo4 {
    public static void main(String[] args) {
        for(int hour=1; hour<24; hour++) {
            for (int minute = 1; minute < 60; minute++) {
                System.out.println(hour+"时" + minute + "分");
            }
        }
    }
}
//一天所有小时和分钟的显示   吗的真妙啊卧槽

Random

import java.util.Random;
public class demo4 {
    public static void main(String[] args) {
        Random r = new Random();//创建对象
        int number = r.nextInt(1000);//获取随机数
        System.out.println(number);
    }
}
//随机数(彩票)

import java.util.Random;
import java.util.Scanner;

public class demo4 {
    public static void main(String[] args) {
        Random r = new Random();//创建对象
        int number = r.nextInt(100)+ 1;//获取随机数
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("输入猜的数字");
            int guessNumber = sc.nextInt();
            if(guessNumber > number){
                System.out.println("大了");

            }else if (guessNumber <number){
                System.out.println("小了");

            }else {
                System.out.println("中了");
            }
        }

    }
}
//随机数(彩票)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值