day2 -练习

找出两个数的最大公约数和最小公倍数:

复制代码
public class day2 {
    //最大公约数,最小公倍数
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入第一个数:");
        int num1 = scanner.nextInt();
        System.out.println("输入第二个数:");
        int num2 = scanner.nextInt();
        int temp = num1 > num2 ? num2 : num1;//找出最小的哪一位
        int MAx = num1 + num2 - temp;
        //求出最大公约数
        int yueShu = 0;
        for (int i = 1; i < =temp; i++) {
            if (MAx % i == 0 && temp % i == 0) {
                yueShu = i;
            }
        }
        System.out.println("最大公约数:" + yueShu);
        //求出最小公倍数
        int gongBei = num1*num2/yueShu;
        System.out.println("最小公倍数:" + gongBei);
    }

}
复制代码

判断两个数是否相等:

比较两个整数是否相同
分析:定义方法实现功能,需要有两个明确,即 返回值 和 参数列表 。明确返回值:比较整数,比较的结果有两种可能,相同或不同,因此结果是布尔类型,比较的结果相 同为true。
明确参数列表:比较的两个整数不确定,所以默认定义两个int类型的参数。

复制代码
class myTest2{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入第一个数:");
        int num1 = scanner.nextInt();
        System.out.println("输入第二个数:");
        int num2 = scanner.nextInt();

        boolean isOk = isOk(num1,num2);
        if (isOk)
            System.out.println("相同");
        else
            System.out.println("不同");
    }
    public static boolean isOk(int num1,int num2){
        return num1==num2;
    }
}
复制代码

 

计算给出的数字,1~这个数的和:

计算1+2+3...+X的和
分析:定义方法实现功能,需要有两个明确,即返回值和参数。
明确返回值:1~X的求和,计算后必然还是整数,返回值类型是int
明确参数:需求中已知到计算的数据,没有未知的数据,不定义参数

复制代码
 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num1 = scanner.nextInt();
        System.out.println("1到"+num1+"的和:"+sum(num1));
    }
    public static int sum(int x){
        int tem=0;
        for (int i = 0; i <= x; i++) {
            tem+=i;
        }
        return tem;
    }
}
复制代码

 

 九九乘法表:

 public static void jiujiu(){
        for (int i = 1; i <=9; i++) {
            for (int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }

 

分数【评级:】

复制代码
class daySwhichTest{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        switch (score/10){
            case 9:
                System.out.println("优秀");
                break;
            case 8:
                System.out.println("良好");
                break;
            case 7:
                System.out.println("一般");
                break;
            case 6:
                System.out.println("及格");
                break;
            default:
                System.out.println("不及格");

        }


    }
}
复制代码
复制代码
class Testss {
    /*String csd[] =new String[10];
        ArrayList<String> strings = new ArrayList<>();
        strings.forEach(System.out::println);*/
    //向左删除:Backspace
    //向右删除:Delete
    //System.out.println("x="+x+";y="+y);//   |  7-2    ||   7-1
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入成绩:");
        int score = scanner.nextInt();
        if (score > 100 || score < 0) {
            System.out.println("成绩无效,请重新输入(0~100)");
            score = scanner.nextInt();
        }
        String jiBie[] = {"优秀", "良好", "一般", "及格", "不及格"};
        if (score >= 90) {
            printArr(jiBie[0]);
        } else if (score >= 80) {
            printArr(jiBie[1]);
        } else if (score >= 70) {
            printArr(jiBie[2]);
        } else if (score >= 60) {
            printArr(jiBie[3]);
        } else {
            printArr(jiBie[4]);
        }

    }

    public static void printArr(String result) {
        System.out.println(result);
    }
}
复制代码

输入年月日,判断今天是今年的第多少天:

复制代码
 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = sc.nextInt();
        System.out.println("请输入月份:");
        int month = sc.nextInt();
        System.out.println("请输入日期:");
        int day = sc.nextInt();

        //判断二月是29天的还是28天的
        int twoMonthDay = 0;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            twoMonthDay = 29;
        } else {
            twoMonthDay = 28;
        }

        int[] days = {31, twoMonthDay, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int resultDay = day;
        for (int i = 0; i < month - 1; i++) {
            resultDay += days[i];
        }
        System.out.println(year + "年的第:" + resultDay + "天");
复制代码

 

给出三个数:从小到大排列:

复制代码
public static void main(String[] args) {
        int num1 =  23;
        int num2 = -19;
        int num3 =  10;
        int temp =0;
        temp =num1>num2?num2:num1;
        num2 =num1+num2-temp;
        num1=temp;

        temp=num3>num2?num2:num3;
        num3=num3+num2-temp;
        num2=temp;

        temp=num1>num2?num2:num1;
        num2 =num1+num2-temp;
        num1=temp;

        System.out.println(num1+"<"+num2+"<"+num3);
}
复制代码

输入年月日,判断哪一个季节:

复制代码
public class FirstJava {
    public static void main(String[] args) {
        // 根据从键盘分别输入的指定月份,打印该月份所属的季节
        String[] jiJie = {"春季", "夏季", "秋季", "冬季"};
        Scanner input = new Scanner(System.in);
        System.out.print("请输入月份(1-12):");
        int month = input.nextInt();
        boolean spring = (month == 3 || month == 4 || month == 5);
        boolean summer = (month == 6 || month == 7 || month == 8);
        boolean autumn = (month == 9 || month == 10 || month == 11);
        boolean winter = (month == 12 || month == 1 || month == 2);
        if (spring) {
            System.out.println(jiJie[0]);
        } else if (summer) {
            System.out.println(jiJie[1]);
        } else if (autumn) {
            System.out.println(jiJie[2]);
        } else {
            System.out.println(jiJie[3]);
        }
    }
    //java 1.8后  变量类型推断:var。。。Java不再是强类型语言。。
}
复制代码

斐波那契:

复制代码
class Fibonacci{
    /**
     * 斐波那契数列(Fibonacci sequence),
     * 又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)
     * 以兔子繁殖为例子而引入,故又称为“兔子数列”,
     * 指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,
     * 斐波那契数列以如下被以递推的方法定义:
     * F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)
     */
    public static void main(String[] args) {
        //波那契数列:1、1、2、3、5、8、13、21、34、……在数学上:后一项等于前两项之和。
        int fibonacciArr[] = new int[20];
        fibonacciArr[0]=1;
        fibonacciArr[1]=1;
        for (int i = 2; i < fibonacciArr.length; i++) {
                fibonacciArr[i]=fibonacciArr[i-1]+fibonacciArr[i-2];
        }
        System.out.println(Arrays.toString(fibonacciArr));
    }
}
复制代码

 

posted on   白嫖老郭  阅读(77)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示