JAVA small practice

A classic three-day fishing program written by Mengxin Xiaobai who only took three Java classes

##First statement:

本人小白,第一次写博客,大佬们请多担待:

##Problem description: There is a Chinese proverb called "three days of fishing and two days of drying the net". Someone started "fishing for three days and drying the net for two days" starting on January 1, 2010, and asked this person whether he would "fishing" or "curing the net" on a certain day in the future.

##Solving ideas: First break down the problem;

  1. First write a function to determine whether it is a leap year
  2. Find out how many leap years there are from 2010 to the keyed year (not including the keyed year) by looping, so as to facilitate subsequent calculations
  3. Make two arrays for whether the keyed year is a leap year, the elements are the days of each month in the leap year and the flat year, and operate on the array to complete the accumulation of the days of the entire month of the keyed year (that is, from 1 to (month-1))
  4. Calculate the total number of days from January 1, 2010 to the date entered
  5. Find the remainder of the total number of days to 5, that is, the total number of days%5. By judging the remainder 1, 2, 3 for fishing, and 0, 4 for drying the net, the final result is obtained.

##Flowchart:
Insert picture description here
##Dry code:

import java.util.Scanner;

public class TiaoshiPrj {
    
    
	public static int year;
	public int month;
	public int day;
	public int bennian;
	public static int x;
    static int count=0;
	
	public void input() {
    
    			//输入函数
		Scanner in = new Scanner(System.in); //定义scanner
		boolean w=true;
		while ( w ) {
    
    
			System.out.println("请输入月:");
			month = in.nextInt();
			if (month<=0 || month>=13) {
    
    
				System.out.println("输入不合法");
			}
			else w=false;
		}
		System.out.println("请输入日:");
		day = in.nextInt();
	}
	
	public static boolean compar(int years) {
    
    	//判断是否是闰年
		
		if((years%4==0&&years%100!=0)||(years%400==0)) {
    
    
			//System.out.println("true");
			return true;
		}
		//else System.out.println("false");
		return false;
	}
	
	public static int numRunyear(int y) {
    
    	//闰年数量
		year=y;
			for ( int i=2010;i<y;i++ ) {
    
    
				if (compar(i)) {
    
    
					count+=1;		//除本年外闰年个数的计数
			    }
			}
			//System.out.println(+count);
			return count;
	}

	public int benNian() {
    
    		//计算本年整月的天数						*****本函数无误
		
		int[] runmonth = {
    
    31,29,31,30,31,30,31,31,30,31,30,31};
		int[] pingmonth = {
    
    31,28,31,30,31,30,31,31,30,31,30,31};
		int sum=0;
		if ( compar(year) ) {
    
    			//闰年
			for (int j=0;j<month-1 ;j++) 
				sum+=runmonth[j];
			bennian=sum;
			//System.out.println("闰");
		}
		else {
    
    					//平年
			for (int j=0;j<month-1 ;j++) 
				sum+=pingmonth[j];
			bennian=sum;
			//System.out.println("平");
		}
		//System.out.println("本年整月天数"+bennian);
		return sum;
	}

	public void dayuShaiwang() {
    
    
		int p=0;
		int pingdays;
		int rundays;
		int numdays;
		p=(year-2010)-count;			//计算平年的个数
		pingdays=p*365;		//计算平年的总天数
		rundays=count*366;			//计算闰年的总天数
		numdays=pingdays+rundays+benNian()+day;	//总天数
		//System.out.println("总天数"+numdays);	//输出总天数
		//if (((numdays%5)==0)&&((numdays%5)==4)) 
			//System.out.println("晒网");
		//else System.out.println("打鱼");
		if (numdays%5 >= 1 && numdays%5 <= 3) 
			System.out.println("在打鱼!");
		
		else 
			if (numdays%5 == 4 || numdays%5 == 0) 
			System.out.println("在晒网!");
			
	}
	
	public static void main(String[] args) {
    
    
		TiaoshiPrj t = new TiaoshiPrj();
		Scanner in = new Scanner(System.in); 
		boolean s=true;
		while ( s ) {
    
    
			System.out.println("请正确输入年");
			t.numRunyear(in.nextInt());
			if(year<2010){
    
     //判断年份
				System.out.println("输入不合法");
			}
			else s=false;
		}
			t.input();
			t.benNian();
			t.dayuShaiwang();
			

	}

}

##The results are as follows:
Insert picture description here
Thank you for watching~

Guess you like

Origin blog.csdn.net/qq_54141095/article/details/115092092