JAVAJ基础练手

Java由浅入深小程序

1、求三个和尚的最高身高Scanner

import java.util.Scanner;
//求三个和尚的身高
public class Demo01 {
   
    public static void main(String[] args) {
   
        //创建scanner对象
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入第一个和尚的身高");
        int height1 = sc.nextInt();

        System.out.println("请输入第二个和尚的身高");
        int height2 = sc.nextInt();

        System.out.println("请输入第三个和尚的身高");
        int height3 = sc.nextInt();


        int meHeight = height1>height2 ? height1:height2;
        int maxHeight = meHeight>height3?meHeight:height3;

        System.out.println("最高的和尚身高是:"+maxHeight+"cm");

    }
}

2、判断奇数还是偶数if…else

import java.util.Scanner;

public class Demo02 {
   
   public static void main(String[] inge){
   
       Scanner sc = new Scanner(System.in);

       //请输入一个随机数字
       System.out.println("请输入一个整数");
       int i =sc.nextInt();

       //判断输入数字的奇数还是偶数
       if((i%2)==0){
   
           System.out.println(i+"是偶数");
       }else{
   
           System.out.println(i+"是奇数");
       }
       System.out.println("请输入整数");

   }
}

3、考试奖励if…else if

import java.util.Scanner;

public class Demo03 {
   
    public static void main(String[] args){
   
            Scanner sc = new Scanner(System.in);

            System.out.println("总分100分,请输入分数:");
            int score = sc.nextInt();

        if(score >100 || score <0){
   
            System.out.println("您输入的分数有误!");
        }else if (score >= 60 && score < 70){
   
            System.out.println("小明考试"+score+"分,不会被打");
        }else if (score < 60){
   
            System.out.println("小明考试"+score+"分,考试不及格!回家要被打");
        }else if (score >=70 && score < 80){
   
            System.out.println("小明考试"+score+"分,获得100块零花钱");
        }else if (score >=80 && score <90){
   
            System.out.println("小明考试"+score+"分,获得获得200块零花钱");
        }else if(score >=90 && score <=100){
   
            System.out.println("小明考试"+score+"分,获得获得300块零花钱");
        }

    }
}

4、输出一个数判断星期几 switch

import java.util.Scanner;

public class Demo04 {
   
    public static void main(String[] args){
   
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入数字【1-7】");
        int week = sc.nextInt();

        switch (week){
   
            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;
            case 7:
                System.out.println("星期天");
                break;
            default:
                System.out.println("输入错误");
                break;
        }

        System.out.println("结束!");

    }
}

5、判断季节 switch

import java.util.Scanner;

public class Demo05 {
   
    public static void main(String[] args){
   

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份");
        //接收输入的月份
        int month = sc.nextInt();

        switch (month){
   
            case 12:
            case 1:
            case 2:
                System.out.println("现在是" + month + "月,属于冬季");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("现在是" + month + "月,属于春季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("现在是" + month + "月,属于夏季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("现在是" + month + "月,属于秋季");
                break;
            default:
                System.out.println("输入有误");
        }


    }
}

6、输出1-5和5-1的数字for

public class Demo06 {
   
    public static void main(String[] args){
   

        for(int i =1;i <= 5;i++){
   
            System.out.println(i);
        
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小城小人物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值