20210411练习
编辑时间:2021/04/15
读完本节:大概花费10钟,共1721词
文章目录
1.输入一个年,月,日,计算是当年的第几天?
import org.junit.Test;
import java.util.Scanner;
/**
* @Author: xuehai.XUE
* @MailBox: xuehai.xue@qq.com
* @Date: 2021/4/15 15:58
* @Description: 输入一个年,月,日,计算是当年的第几天
*/
public class CountDate {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//获取输入的年份
String yearStr;
System.out.print("输入 年份:");
//获取的年份数字若为 0125,最后自动读为 125
int year = scan.nextInt();
//年份合法性检验
boolean flag = false;
if (year == 0) {
flag = true;
}
while (flag) {
System.out.print("输入正确的 年份:(不为0)");
year = scan.nextInt();
if (year == 0) {
flag = true;
} else {
flag = false;
}
}
//yearStr赋值
if (year < 0) {
yearStr = "公元前 " + (-year);
} else {
yearStr = "公元 " + (year);
}
// System.out.println(yearStr);
// //将年份转化为字符串取首位的方法
// String yearStr = "" + year;
// int y = Integer.parseInt("" + yearStr.charAt(0));
// System.out.println(y);
//获取输入的月份
System.out.print("输入 月份:");
int month = scan.nextInt();
//月份合法性检验
while (month <= 0 || month > 12) {
System.out.print("输入正确的 月份:[1,12]");
month = scan.nextInt();
}
//获取输入的日期
System.out.print("输入 日期:");
int date = scan.nextInt();
//日期合法性检验
if (year < 0) {//公元前,没有公元0年,公元1年称为公元元年,所以若为公元前221年应当是闰年
year = year + 1;
//不复用的原因是便于阅读的连续性
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//年份判断:闰年
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
//date==31
while (date <= 0 || date > 31) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~31])");
date = scan.nextInt();
}
} else if (month != 2) {
//date==30
while (date <= 0 || date > 30) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~30])");
date = scan.nextInt();
}
} else {
//date=29
while (date <= 0 || date > 29) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~29])");
date = scan.nextInt();
}
}
} else {//年份判断:平年
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
//date==31
while (date <= 0 || date > 31) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~31])");
date = scan.nextInt();
}
} else if (month != 2) {
//date==30
while (date <= 0 || date > 30) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~30])");
date = scan.nextInt();
}
} else {
//date=28
while (date <= 0 || date > 28) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~28])");
date = scan.nextInt();
}
}
}
year--;
} else if (year > 0) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//年份判断:闰年
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
//date==31
while (date <= 0 || date > 31) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~31])");
date = scan.nextInt();
}
} else if (month != 2) {
//date==30
while (date <= 0 || date > 30) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~30])");
date = scan.nextInt();
}
} else {
//date=29
while (date <= 0 || date > 29) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~29])");
date = scan.nextInt();
}
}
} else {//年份判断:平年
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
//date==31
while (date <= 0 || date > 31) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~31])");
date = scan.nextInt();
}
} else if (month != 2) {
//date==30
while (date <= 0 || date > 30) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~30])");
date = scan.nextInt();
}
} else {
//date=28
while (date <= 0 || date > 28) {
System.out.print("输入正确的 日期:(" + month + "月份日期范围是[1~28])");
date = scan.nextInt();
}
}
}
}
//输出计算这天是该年的第几天
int sumDays = 0;
switch (month) {
case 12:
sumDays += 30;//这里加的30是11月的
case 11:
sumDays += 31;//这里加的31是10月的
case 10:
sumDays += 30;//同上,下同
case 9:
sumDays += 31;
case 8:
sumDays += 30;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
if (year < 0) {//公元前,没有公元0年,公元1年称为公元元年,所以若为公元前221年应当是闰年
year = year + 1;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//判断闰年
sumDays += 29;
} else {
sumDays += 28;
}
year--;//恢复对数据的处理
} else if (year > 0) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//判断闰年
sumDays += 29;
} else {
sumDays += 28;
}
}
case 2:
sumDays += 31;
case 1:
sumDays += date;
}
//输出结果
System.out.println(yearStr + " 年 " + month + " 月 " + date + " 日 是当年的第 " + sumDays + " 天。");
if (year < 0) {//公元前,没有公元0年,公元1年称为公元元年,所以若为公元前221年应当是闰年
year = year + 1;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//判断闰年
System.out.println(yearStr + " 是闰年。");
} else {
System.out.println(yearStr + " 是平年。");
}
year--;//恢复对输入数据year的处理
} else if (year > 0) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {//判断闰年
System.out.println(yearStr + " 是闰年。");
} else {
System.out.println(yearStr + " 是平年。");
}
}
}
}
2.输入三个数,找出其中的最大值,最小值?
import java.util.Arrays;
import java.util.Scanner;
/**
* @Author: xuehai.XUE
* @MailBox: xuehai.xue@qq.com
* @Date: 2021/4/15 16:14
* @Description: 输入三个数,找出其中的最大值,最小值
*/
public class FindMax {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("first number: ");
int first = scanner.nextInt();
System.out.print("second number: ");
int second = scanner.nextInt();
System.out.print("third number: ");
int third = scanner.nextInt();
//寻找最大值
int max = first;
if(max < second){
max = second;
}
if(max < third){
max = third;
}
//寻找最小值
int min = first;
if(min > second){
min = second;
}
if(min > third){
min = third;
}
System.out.println("最小值是: " + min);
System.out.println("最大值是: " + max);
}
}
3.求1+2!+3!+…+20!的和?
import java.math.BigInteger;
/**
* @Author: xuehai.XUE
* @MailBox: xuehai.xue@qq.com
* @Date: 2021/4/15 16:22
* @Description: 求1+2!+3!+...+20!的和
*/
public class SumUp {
public static void main(String[] args) {
final int MAX_BOUND = 20;
BigInteger sum = new BigInteger("1");
BigInteger value = new BigInteger("0");
for(int i = 1; i <= MAX_BOUND;i++){
sum = sum.multiply(BigInteger.valueOf(i));
value = value.add(sum);
}
System.out.println("1+2!+3!+...+20! = " + value);
}
}
4.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
import java.math.BigDecimal;
/**
* @Author: xuehai.XUE
* @MailBox: xuehai.xue@qq.com
* @Date: 2021/4/15 16:52
* @Description: 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
*/
public class SumSeq {
public static void main(String[] args) {
final int MAX_DENOMINATOR = 20;
//初始分子
double numberator = 2.0;
//初始分母
double denominator = 1.0;
//分式加和结果
double value = 0.0;
for (int i = 0; i < MAX_DENOMINATOR;i++){
value += numberator / denominator;
numberator += denominator;
denominator = numberator - denominator;
}
System.out.println("分式前20个值的结果是:" + value);
}
}
5.猜拳游戏
import org.junit.Test;
import java.util.Random;
import java.util.Scanner;
/**
* @Author: xuehai.XUE
* @MailBox: xuehai.xue@qq.com
* @Date: 2021/4/15 17:21
* @Description: 猜拳游戏
*/
public class Mora {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("请输入猜拳(石头、剪刀、布):");
String fist = sc.nextLine();
String[] a = {"石头","剪刀","布"};
boolean f = true;
Random r = new Random();
int count = 0;
int stopNum = r.nextInt(100)+10;
while(f) {
int index = r.nextInt(3);
//在java的输出控制中\r是回到开头,\n是换行,在输出的过程中不用println,而是用\r\n这些控制符控制回车换行
System.out.print(a[index] + "\r");
if(stopNum == count) {
System.out.println("结果是: " + a[index]);
if("石头".equals(fist)){
if(a[index].equals("石头")){
System.out.println("draw");
}else if(a[index].equals("剪刀")){
System.out.println("You win");
}else{
System.out.println("You lose");
}
}
if("剪刀".equals(fist)){
if(a[index].equals("剪刀")){
System.out.println("draw");
}else if(a[index].equals("布")){
System.out.println("You win");
}else{
System.out.println("You lose");
}
}
if("布".equals(fist)){
if(a[index].equals("布")){
System.out.println("draw");
}else if(a[index].equals("石头")){
System.out.println("You win");
}else{
System.out.println("You lose");
}
}
break;
}
count++;
}
}
}