java_note4

基础

变量 常量 输出 注释 关键字 保留字 类的结构 运算符 表达式

java.util.Date

java.util.Calendar

java.time.LocalDateTime

java.time.LocalDate

java.time.LocalTime

java.time.Random

if语句
类的使用?
1>引入import(java.lang.*,同包)
2>实例化 Xxx aa = new Xxx()
  • Math.random()

  • Calendar.getInstance()

  • LocalDate.now() .of()

3> 使用实例对象的方法

分支语句

1.if判断语句

  • if()语句;
  • if(){语句1;语句2;…}
  • if(){}else{}

1、if编写分时问候程序

package cn.practice;

import java.util.Calendar;

public class If1 {
    public static void main(String[] args) {
        //if else  else if
        //分支语句 if()语句;if(){语句1;语句2;...};if(){}else{};if(){}else if(){}else if(){}else{}
        int i = 5;
        if(i>6)
            System.out.println("ok");//不规范
        if (i>6){
            System.out.println("ok");
        }else {
            System.out.println("no");
        }
        //使用if语句输出上午  下午
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        if(hour >= 12){
            System.out.println("下午");
        }else {
            System.out.println("上午");
        }
    }
}
  • if(){}else if(){}else{}
package cn.practice;

import java.util.Calendar;

public class If2 {
    public static void main(String[] args) {
        //输出星期几使用if语句
        Calendar c = Calendar.getInstance();

        //week 范围=1-7 1代表周日 2 周一
        int week = c.get(Calendar.DAY_OF_WEEK);

        //如下效率不好
        /*if (week==1) System.out.println("星期日");
        if (week==2) System.out.println("星期一");
        if (week==3) System.out.println("星期二");
        if (week==4) System.out.println("星期三");
        if (week==5) System.out.println("星期四");
        if (week==6) System.out.println("星期五");
        if (week==7) System.out.println("星期六");*/

        if (week == 1) {
            System.out.println("星期日");
        } else if (week == 2) {
            System.out.println("星期一");
        } else if (week == 3) {
            System.out.println("星期二");
        } else if (week == 4) {
            System.out.println("星期三");
        } else if (week == 5) {
            System.out.println("星期四");
        } else if (week == 6) {
            System.out.println("星期五");
        } else {
            System.out.println("星期六");
        }
        System.out.printf("%tA%n", c);

        //一月
        System.out.printf("%tm", c);

        int m = c.get(Calendar.MONTH);
        int m1 = c.get(2);
        if (m == 0) {
            System.out.println("一月");
        } else if (m == 1) {
            System.out.println("二月");
        } else if (m == 2) {
            System.out.println("三月");
        } else if (m == 3) {
            System.out.println("四月");
        } else if (m == 4) {
            System.out.println("五月");
        } else if (m == 5) {
            System.out.println("六月");
        } else if (m == 6) {
            System.out.println("七月");
        } else if (m == 7) {
            System.out.println("八月");
        } else if (m == 8) {
            System.out.println("九月");
        } else if (m == 9) {
            System.out.println("十月");
        } else if (m == 10) {
            System.out.println("十一月");
        } else {
            System.out.println("十二月");
        }
    }
}
if语句小练习

判断int y = 2019;这个年份是不是闰年,程序输出闰年和平年?

package cn.practice;

public class OrdinaryLeap {
    public static void main(String[] args) {
        //判断int y = 2019;这个年份是不是闰年,程序输出闰年和平年?
        int y = 1000;
        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
            System.out.println("闰年");
        } else {
            System.out.println("平年");
        }
    }
}
三元运算表达式,可以简单代替if(){}else{}语句
package cn.practice;

import java.util.Calendar;

public class TernaryIf {
    public static void main(String[] args) {
        Calendar c=Calendar.getInstance();
        int h = c.get(Calendar.HOUR_OF_DAY);
        String s = h<12?"上午":"下午";
        System.out.println(s);
    }
}

2.switch语句

switch case break default -> yield
package cn.practice;

import java.util.Calendar;

public class Switch1 {
    public static void main(String[] args) {
        //switch 多分支开关语句
        //int n = Calendar.getInstance().get(7);
        //1-7

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE,-1);
        int n = c.get(7);
        
        switch (n) {
            case 1:
                System.out.println("星期日");
                break;
            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;
            default:
                System.out.println("星期六");
                break;
        }
    }
}
面试题:
switch语句支持哪些类型?

char(包装类Character),byte,short,int,String,enum

在这里插入图片描述

switch case break default

switch expresssion

注意:switch 表达式必须有default,如果有多行语句,则在{yield返回赋值}

2、switch语句及表达式使用,根据指定的年月计算输出该月有多少天?

switch指定年月,输出xxxx年xxxx月有xx天?

package cn.practice;

public class Switch2 {
    public static void main(String[] args) {
        int year=2023;
        int month = 2;
        int days=31;
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            default:
                days=year%400==0||year%4==0&&year%100!=0?29:28;
                break;
        }
        System.out.printf("%d年%d月有%d天%n",year,month,days);
    }
}
package cn.practice;

public class Switch3 {
    public static void main(String[] args) {
        int year=2008;
        int month=2;
        int days=switch (month){
            case 1,3,5,7,8,10,12 ->31;
            case 4,6,9,11 -> 30;
            case 2 -> year%400==0||year%4==0&&year%100!=0?29:28;
            default -> 31;
        };
        System.out.printf("%d年%d月有%d天%n",year,month,days);
    }
}

3.循环语句

1.for for each

在这里插入图片描述

for 计数循环语句
package cn.practice;

public class For1 {
    public static void main(String[] args) {
        //计算1+2+3+...+100=?
        int sum=0;
        for (int i=1;i<=100;i++){
            sum+=i;
        }
        System.out.printf("1+2+3+...+100=%d%n",sum);

        //计算1+2+3+...+100=?
        System.out.println(101*50);
        //数学家总结的公式

        //计算1+2+3+...+100=?(不能使用循环语句、递归方法,不能直接写结果)

        //死循环
        /*for (;;){
            System.out.println("ok");
        }*/

        //死循环
        //for (;;);

        //for (循环变量的初始化;循环条件;增量){}
        //循环语句结束是循环条件为假时结束,或者在循环体中的语句有break执行
        for (int i=1;i<=5;i++){

            System.out.printf("%d ",i);
        }
        System.out.println();
        for (int i=5;i>=1;--i){
            System.out.printf("%d ",i);
        }
    }
}
package cn.practice;

public class For2 {
    public static void main(String[] args) {
        int i =1;
        for (;i<10;){
            System.out.printf("%d ",i++);//1 2 3 4 5 6 7 8 9
        }
        System.out.println();
        System.out.println(i);//10
        System.out.println("\r\n"+i);//换行输出
    }
}
1-100偶数和
package cn.practice;

public class HundredEvenSum {
    public static void main(String[] args) {
        int s=0;
        for (int i = 1; i <= 100; i++) {
            if (i%2==0){
                s+=i;
            }
        }
        System.out.println(s);//2550
    }
}

3、打印九九乘法表

9*9乘法表
package cn.practice;

public class MultiplicationTable {
    public static void main(String[] args) {
        for (int a=1;a<=9;a++){
            for (int b=1;b<=a;b++){
                if (a==4&&b==3){
                    System.out.printf("%2d*%-2d=%3d ",a,b,a*b);
                    //System.out.printf("%d * %d = %d\t",a,b,a*b);
                } else if (a*b>=10) {
                    System.out.printf("%d*%-2d=%3d ",a,b,a*b);
                } else if (b==2) {
                    System.out.printf("%d*%-2d=%2d ",a,b,a*b);
                }else {
                    System.out.printf("%2d*%-2d=%2d ",a,b,a*b);
                }
            }
            System.out.println();
        }
    }
}

在这里插入图片描述

4、打印菱形图案

package cn.practice;

public class DiamondShape {
    public static void main(String[] args) {
        //菱形*
        int s=20;
        for (int i=1;i<20;i+=2){
            s--;
            for (int t=s;t>=1;t--){
                System.out.print(" ");
            }
            for (int n=1;n<=i;n++){
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i=17;i>=1;i-=2){
            s++;
            for (int t=s;t>=1;t--){
                System.out.print(" ");
            }
            for (int n=1;n<=i;n++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

6、百钱百鸡算法

package cn.practice;

public class HundredChickens {
    public static void main(String[] args) {
        //计算百钱百鸡-用判断语句
        //利用三层循环-4个答案
        //鸡翁一只五钱,鸡母一只三钱,鸡雏三只一钱。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何
        for(int a = 0;a<=20;a++){
             for (int b = 0;b<=33;b++){
                 for (int c = 0;c<=100;c++){
                     if (c%3!=0){continue;}
                     int s1 = a+b+c;
                     int s2 = a*5+b*3+c/3;
                     if (s1 == 100 && s2 == 100){
                         System.out.printf("%d %d %d%n",a,b,c);
                     }
                 }
             }
        }

        for (int x=0;x<=20;x++){
            for (int y=0;y<=33;y++){
                for (int z=0;z<=100;z+=3){
                    if (x+y+z==100&&x*5+y*3+z/3==100){
                        System.out.printf("%d %d %d%n",x,y,z);
                    }
                }
            }
        }
    }
}
运算结果:
0 25 75
4 18 78
8 11 81
12 4 84
for each
package cn.practice;

import java.util.List;

public class ForEach {
    public static void main(String[] args) {
        //声明int[] 数组
        int[] nums = {1,2,3,4,5,6};
        for (int i=0;i< nums.length;i++){
            System.out.print(nums[i]+" ");
        }
        System.out.println();
        for (int i = nums.length-1;i>=0;i--){
            System.out.print(nums[i]+" ");
        }
        System.out.println();

        //for each for:
        for (int i:new int[]{1,2,3}){
            System.out.println(i);
        }

        List<String> list = List.of("java","html","mysql","javascript");
        for (var s:list){
            System.out.println(s);
        }

        for (int i = list.size()-1;i>=0;i--){
            System.out.println(list.get(i));
        }
    }
}

在这里插入图片描述

打印系统的语句
package cn.practice;

import java.util.Set;

public class ForEach1 {
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.version"));
        Set<Object> ks = System.getProperties().keySet();
        for (Object k:ks){
            System.out.println(k+"="+System.getProperty(k.toString()));
        }
    }
}

在这里插入图片描述

在这里插入图片描述

2.which

条件循环语句

while(条件){

}

条件循环,条件为true进入循环,每次循环都要看条件,为false时,退出循环。

package cn.practice;

public class While1 {
    public static void main(String[] args) {
        int i = 1;
        while(i<=5){
            System.out.println(i++);
        }
        --i;
        while(i>=1){
            System.out.printf("%03d\t",i);
            --i;
        }
    }
}

在这里插入图片描述

5、编写猜数游戏

输入 判断 循环

package cn.practice;

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

public class GuessNum {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Random rand = new Random();
        int t = rand.nextInt(1,101);
        while (true){
            System.out.print("请输入整数[1-100]:");
            int n = sc.nextInt();
            if (n>t){
                System.out.printf("太大了%n");
            }else if(n<t){
                System.out.printf("太小了%n");
            }else{
                System.out.printf("恭喜,猜对了%n");
                break;
            }
        }
        System.out.println("游戏结束");
    }
}
package cn.practice;

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

public class GuessNumHard {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        int[] level = {100,200,500,1000,5000};
        int le = level[rand.nextInt(level.length)];
        int t = rand.nextInt(1,le+1);
        int num = 0;
        while(true){
            System.out.print("\033[34m请输入整数[1-"+le+"]:\033[0m");
            int n = sc.nextInt();
            ++num;
            if (n>t){
                System.out.printf("\033[31m%d、太大了\033[0m%n",num);
            } else if (n<t) {
                System.out.printf("\033[31m%d、太小了\033[0m%n",num);
            }else{
                System.out.printf("\033[32m恭喜,猜对了,游戏  \033[31m %d\033[0m \033[32m分\033[0m%n",110 - num * 10);
                break;
            }
        }
        System.out.println("\033[32m游戏结束\033[0m");
    }
}
3.do while

do {

}while(条件);

最少循环一次,无条件进入循环语句,条件为true再循环一次,如果条件为false,结束循环

package cn.practice;

public class Dowhile {
    public static void main(String[] args) {
        int i = 1;
        do{
            System.out.println(i);//1
            ++i;
            break;
        }while (i<=10);
    }
}
需掌握的语句
  • if else

  • switch case break default yield

    for(;😉{

    }

  • for(var i:int[]){

    }

  • while(条件){

    }

  • do{

    }while();
    3[0m \033[32m分\033[0m%n",110 - num * 10);
    break;
    }
    }
    System.out.println(“\033[32m游戏结束\033[0m”);
    }
    }


#### 3.do while

> do {
>
> 
>
> }while(条件);
>
> 最少循环一次,无条件进入循环语句,条件为true再循环一次,如果条件为false,结束循环

```java
package cn.practice;

public class Dowhile {
    public static void main(String[] args) {
        int i = 1;
        do{
            System.out.println(i);//1
            ++i;
            break;
        }while (i<=10);
    }
}
需掌握的语句
  • if else

  • switch case break default yield

    for(;😉{

    }

  • for(var i:int[]){

    }

  • while(条件){

    }

  • do{

    }while();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值