自学Java day01

Java的基础语法,数据类型、类型转换、运算符等,

if条件控制语句

import java.util.Scanner;

public class ifscore{
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        
        int score = sc.nextInt();
        
        if(score >= 0 && score <= 100){
            if(score >= 90 && score <= 100){
                System.out.println("自行车一辆");
            }else if (score >= 80 && score <= 89){
                System.out.println("游乐场一次");
            }else if (score >= 70 && score <= 79){
                System.out.println("变形金刚一个");
            }else if (score >= 60 && score <= 69){
                System.out.println("没有奖励");
            }else{
                System.out.println("挨顿揍");
            }
        }else{
            System.out.println("请输入正确成绩!");
        }
        
        
    }
}

switch语句

switch(week){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("工作日");
            case 6:
            case 7:
                System.out.println("休息日");

while和for的循环语句

for用于已知循环次数

while用于不知道循环次数

用for语句编写个位十位百位和水仙花数的程序

for(int i = 100;i <= 999;i++){
            
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;
            
            if((ge*ge*ge + shi*shi*shi + bai*bai*bai) == i){
                System.out.print(i +" ");
                count++;
                
                if(count % 2 == 0){
                    System.out.println();

while的三种循环和死循环

random程序/需求:1-10随机出现数字

import java.util.Random;

public class Damo1Random{
    public static void main(String[] args){
        Random r = new Random();
        for(int i = 1; i <= 10;i++){
            int num = r.nextInt(10) + 1;//1-10
            System.out.println(num);
        }
    }
}

跳转语句/需求:避开4楼

public class Damo1continue{
    public static void main(String[] args){
        for(int i = 1;i <=24;i++){
            if(i == 4){
                continue;
            }
            System.out.println(i + "层到了");
        }
    }
}

编程题/需求在0-100中随机出现一个数

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

public class Test1{
    public static void main(String[] args){
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        
        int randomNum = r.nextInt(100) + 1;
        
        System.out.println(randomNum);
        
        while(true){
            System.out.println("请输入您要猜的数据:");
            int num = sc.nextInt();
        
            if(num > randomNum){
                System.out.println("猜大了");
            }else if(num < randomNum){
                System.out.println("猜小了");
            }else{
                System.out.println("恭喜猜中了");
                break;
            }
        }
        System.out.println("感谢您的使用");
        
    }
}

注释:Scanner 键盘录入 Random 随机数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值