JAVA数据判断代码参考

本文介绍了使用JAVA编写的四个示例:判断数字大小、判断日期(闰年和平年)、生成不同数字组合的三位数以及一维和二维数组操作。通过实例展示了如何在JAVA中进行基本的数据比较和数组处理。
摘要由CSDN通过智能技术生成

1. 判断数字大小

package week2.day6;

import java.util.Scanner;

public class three {
    public static void main(String[] args) {
        //输入三个整数x,y,z,请把这三个数由小到大输出
        Scanner input = new Scanner(System.in);
        System.out.println("请输入a的值");
        int a = input.nextInt();
        System.out.println("请输入b的值");
        int b = input.nextInt();
        System.out.println("请输入c的值");
        int c = input.nextInt();
        int temp;
        if(a>b) {
            temp = a;
            a=b;
            b=temp;

        }
        if(a>c) {
            temp = a;
            a= c;
            c=temp;

        }
        if(b>c) {
            temp= b;
            b=c;
            c=temp;
        }
        System.out.println(a+"<"+b+"<"+c);
    }
}

2. 判断日期

package week6.day8;

import java.util.Scanner;

public class nianyueri {
    public static void main(String[] args) {
        demo01();

    }
    public static void demo01() {
        int[] leapYear= {0,31,60,91,121,152,182,213,244,274,305,335,366};//闰年
        int[] commonYear= {0,31,59,90,120,151,181,212,243,273,304,334,365};//平年
        Scanner input=new Scanner(System.in);
        System.out.print("请输入您要查询的年份:");
        int year=input.nextInt();
        System.out.print("请输入您要查询的月份:");
        int month=input.nextInt();
        System.out.print("请输入您要查询的天数:");
        int day=input.nextInt();
        int sumDays=0;
        if(month>12||month<1) {
            System.out.println("请输入正确的月份!");
        }else{
            //判断是否为闰年
            if((year%4==0&&year%100!=0)||year%400==0) {
                //天数为31天的月份
                if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
                    //判断天数是否在31天之内
                    if(day<1||day>31) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=leapYear[month-1]+day;
                    }
                }else if(month==2) {
                    //判断天数是否在29天之内
                    if(day<1||day>29) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=leapYear[month-1]+day;
                    }
                }else {//为4、6、9、11月中的一月
                    //判断天数是否在30天之内
                    if(day<1||day>30) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=leapYear[month-1]+day;
                    }
                }
            }else {
                //为平年
                //天数为31天的月份
                if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
                    //判断天数是否在31天之内
                    if(day<1||day>31) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=commonYear[month-1]+day;
                    }
                }else if(month==2) {
                    //判断天数是否在28天之内
                    if(day<1||day>28) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=commonYear[month-1]+day;
                    }
                }else {//为4、6、9、11月中的一月
                    //判断天数是否在30天之内
                    if(day<1||day>30) {
                        System.out.println("请输入正确的天数!");
                    }else {
                        sumDays=commonYear[month-1]+day;
                    }
                }
            }
        }
        System.out.println("这一天为一年中的第"+sumDays+"天");
    }
}

3. 判断数字组合

package week3.day4;

public class nums {
    public static void main(String[] args) {
        //有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少
        int m=0;
            for(int i=1;i<=4;i++){
                for(int j=0;j<=4;j++){
                    for(int k=0;k<=4;k++){
                        if(i!=j&&j!=k&&i!=k){
                            m++;
                            System.out.print("第"+m+"个数字为:");
                            System.out.print(i);
                            System.out.print(j);
                            System.out.println(k);
                        }}}}
    }
}

4. 数组参考

package week3.day6;
import java.util.ArrayList;
import java.util.Iterator;

public class shuzu {
    public static void main(String[] args) {
        //一维数组
        int shu = 5;
        int[] shu1 =new int[shu];
        shu1[1]=2542;
        shu1[2]=26;
        for (int i:shu1){
            System.out.print(i+" ");
        }
        System.out.print('\n');

        //二维数组
        int [][] erwei = new int[2][3];
        erwei[0][2] = 1;
        erwei[0][1] = 1;
        erwei[1][2] = 2;
        erwei[1][1] =6;
        for (int[] i1:erwei){
            for (int[] i2:erwei){
                System.out.print(i1+" ");}
        }
        System.out.print('\n');
        //for (int s=0;s<=erwei.length-1;s++)
        //    {for (int k=0;k<=erwei.length-1;k++){
        //        System.out.print(s);
        //        System.out.println(k);}}

        //判断符合条件的数组
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(12);
        numbers.add(8);
        numbers.add(2);
        numbers.add(7);
        numbers.add(23);
        Iterator<Integer> it = numbers.iterator();
        while(it.hasNext()) {
            int i = it.next();
            if(i > 12) {
                it.remove();}}
        System.out.println(numbers);}
}

在这里欢迎大家的点赞、关注、评论,以此来促进大家互相学习交流,同时可以让新加入的小伙伴更快的了解新知识!!!

文章内容如有侵权,请联系作者进行删除

≧◠◡◠≦ 1分2分都是爱,感谢已经打赏的老板,和正在打赏的老板们 ≧◠◡◠≦

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TinyTuiKun

感谢各位老板们的打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值