一,百钱买百鸡问题

我国古代数学家张丘建在《算经》一书中曾提出过著名的“百钱买百鸡”问题,该问题叙述如下:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一;百钱买百鸡,则翁、母、雏各几何?`

public class Test {
    public static void main(String[] args) {
        int q = 0;
        for (int w = 0; w <= 20; w++) {
            for (int e = 0; e <= 33; e++) {
                int r = 100 - w - e;
                if (r % 3 == 0 && (5 * w + 3 * e + r / 3 == 100)) {
                    q = ++q;
                    System.out.println("方案" + q);
                    System.out.println("公鸡" + q + "母鸡" + e + "小鸡" + r);
                }
            }
        }
    }
}

Java小程序

二,多次输入数字,得到其对应月份

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        int month;
        Scanner user=new Scanner(System.in);
        System.out.println("请输入一个月份数字");
        while (user.hasNext()){
            month=user.nextInt();
            switch (month) {
                case 1:
                    System.out.println(month + "属于春季");
                    break;
                case 2:
                    System.out.println(month + "属于春季");
                    break;
                case 3:
                    System.out.println(month + "属于春季");
                    break;
                case 4:
                    System.out.println(month + "属于夏季");
                    break;
                case 5:
                    System.out.println(month + "属于夏季");
                    break;
                case 6:
                    System.out.println(month + "属于夏季");
                    break;
                case 7:
                    System.out.println(month + "属于秋季");
                    break;
                case 8:
                    System.out.println(month + "属于秋季");
                    break;
                case 9:
                    System.out.println(month + "属于秋季");
                    break;
                case 10:
                    System.out.println(month + "属于冬季");
                    break;
                case 11:
                    System.out.println(month + "属于冬季");
                    break;
                case 12:
                    System.out.println(month + "属于冬季");
                    break;
                default:
                    System.out.println("输入的数字不在1-12内,请重新输入");
            }
        }
    }
}

Java小程序

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-09-06
  • 2021-09-15
  • 2021-06-22
  • 2022-01-15
猜你喜欢
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2021-12-27
  • 2021-12-13
相关资源
相似解决方案