java实验二 Java基础知识理解

实验内容:

1、编写一个控制台程序,输入一个日期,求下一天的日期。要求如下:
(1)在控制台输入一个日期(分别输入年、月、日),判断输入的日期是否有效,如果有效,计算该日期的下一天日期,并显示;否则,输出“无效的日期”后退出

2、使用Scanner和System.in输入各个变量的值,并按照公式计算得出结果,公式如下:

要求如下:
(1)输入三组不同的x、y和z的值,计算结果

内容一代码

//lsy
package java_experiments;

import java.util.Scanner;

public class two_one {

	public static void NextDate() {
		int year, month, day;
		int year1, month1, day1;
		int run = 0;
		// @SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		// while(true) {
		while (true) {
			System.out.println("请输入年份:");
			year = input.nextInt();
			if (year < 1000 || year > 9999) {
				System.out.println("输入数据非法");
				continue;
			}
			if ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0)
				run = 1;
			else
				run = 0;
			break;
		}
		while (true) {
			System.out.println("请输入月份");
			month = input.nextInt();
			if (month > 12 || month < 1) {
				System.out.println("输入数据非法");
				continue;
			}
			break;
		}
		while (true) {
			System.out.println("请输入日期");
			day = input.nextInt();
			if (day > 31 || day < 1)

			{
				System.out.println("输入数据非法");
				continue;
			}
			if (day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)) {
				System.out.println("输入日期不存在");
				continue;
			}

			if (day == 30 && month == 2) {
				System.out.println("输入日期不存在");
				continue;
			}
			if (day == 29 && run == 0 && month == 2) {
				System.out.println("输入日期不存在");
				continue;
			}
			break;
		}

		if (day == 31 || (day == 30 && (month == 4 || month == 6 || month == 9 || month == 11))) {
			if (month == 12) {
				month1 = 1;
				year1 = year + 1;
				day1 = 1;
			} else {
				month1 = month + 1;
				year1 = year;
				day1 = 1;
			}
		} else if ((day == 29 && month == 2) || (day == 28 && month == 2 && run == 0)) {
			month1 = 3;
			year1 = year;
			day1 = 1;
		} else {
			month1 = month;
			day1 = day + 1;
			year1 = year;
		}
		System.out.println("您输入的是" + year + "年" + month + "月" + day + "日");
		System.out.println("它的下一天是" + year1 + "年" + month1 + "月" + day1 + "日\n");
		input.close();
	}

	// }
	public static void main(String[] args) {
		NextDate();
	}

}

实验结果

在这里插入图片描述

内容二代码:

//lsy
package java_experiments;

import java.util.Scanner;

public class two_two {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("输入三个数,按顺序为:x,y,z:");
		float x = input.nextFloat();
		float y = input.nextFloat();
		float z = input.nextFloat();
		if (x + y * z != 0) {
			float number = (3 * x + (4 * y * y + 5) / 2 - 7 * (z + 4) + (3 + x * x) / (x + y * z));
			System.out.println("公式运算结果为:" + number);
		} else
			System.out.println("输入的数据无法满足公式的计算");
		input.close();
	}
}

实验结果

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值