java简单代码

今日学习
java简单代码

public class 輸入一到一百的数字判断是不是质数{
	// 除了自己之和一之外不能被整除的數--質數
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i++) {
			if (abc(i)) {
				System.out.println(i + "shi zhishu");

			} else
				System.out.println(i + "bushizhishu");
		}
		System.out.println(abc(10));

	}

	public static boolean abc(int kk) {
		boolean res = true;
		for (int i = 2; i <= kk / 2; i++) {
			if (kk % i == 0) {
				res = false;
				break;
			}
		}
		return res;
	}
}
public class 输入日期计算日期在本年的天数 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = 0, months = 0, date = 0;
		while (true) {
			year = intputNum(sc, 1, 3000, "年份");
			months = intputNum(sc, 1, 12, "月份");
			date = intputNum(sc, 1, 30, "日期");
			boolean bb = validateDate(year, months, date);
			if (bb)
				break;
			System.out.println("请输入正确年月日");
		}
		System.out.println(year + "-" + months + "-" + date);
	}
	public static boolean validateDate(int year, int months, int date) {
		boolean res = false;
		switch (months) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			res = date >= 1 && date <= 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			res = date >= 1 && date <= 30;
			break;
		case 2:
			boolean bb = run(year);
			if (bb)
				res = date <= 29 && date >= 1;
			else
				res = date <= 28 && date >= 1;
			break;
		default:
			res = false;
			break;
		}
		return res;
	}
	public static boolean run(int year) {
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}
	public static int intputNum(Scanner sc, int min, int max, String str) {
		int res = 0;
		while (true) {
			System.out.println(str + ":");
			String ss = sc.nextLine();
			try {
				res = Integer.parseInt(ss);
				if (res >= min && res <= max)
					break;
				System.out.println("请输入合理的" + str + "值");	
			} catch (Exception e) {
				System.out.println("您输入的日期不合法");
			}
		}
		return res;  
	}
}
import java.util.Scanner;

public class 手写兔子 {
	public static void main(String[] args) {
		int months = 0;
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("月份");
			String ss = sc.nextLine();
			try {
				months = Integer.parseInt(ss);
				if (months > 0)
					break;
				System.out.println("请重新输入月份");

			} catch (Exception e) {
				System.out.println("请输入合法的月份值");
			}
		}
		int num = tongJi(months);
		System.out.println(months + "月后的兔子数为:" + num);
	}
	public static int tongJi(int months) {
		if (months > 0) {
			if ((months == 1) || (months == 2))
				return 1;
			return tongJi(months - 1) + tongJi(months - 2);
		}
		return 0;
	}
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值