作業說明:
中國有句俗語叫“三天打魚兩天曬網”。某人從2010年1月1日起開始“三天打魚兩天曬網”,問這個人在以後的某一天中是“打魚”還是“曬網”。用C或C++語言/java/python實現程序解決問題。
解決“三天打漁 兩天曬網”的過程,:
1.計算從2010年1月1日開始至指定日期共有多少天。
2.由於“打漁”和“曬網”的週期爲5天,所以將計算出的天數用5去除。
如果餘數是0,1,2,則是“打漁”,否則爲“曬網”。
而要計算指定日期到2010年的天數,首先要搞清楚每一月有幾天,當閏年時2月的天數也不同,因此要判斷指定日期是否是閏年和月份。
如果指定日期是2010以後的日期,用364+到指定日期前一年經過的天數+指定日期當年的經過的天數。
判斷閏年的方法:
1、能整除4且不能整除100
2、能整除400
剛開始我用鍵盤輸入年月日,是分開輸入的,沒有判定輸入日期正確與否,可以判斷打漁或是曬網。
後面我用文件讀入時遇到了幾個問題。
1.讀入的數字是一連串的日期,沒有年月日之分。所以我用了一個集合來接收數據,並用了substring函數來分割其中的數字賦值給年月日。
2.文件讀入後不知道怎麼輸出文件,因爲判定judge函數裏的返回值是true和false,我打印時不知道該輸出什麼,後面我把返回值改成string類型,並返回“打漁”或是“曬網”。
3.文件輸出時,只輸出了一個日期的結果,後面調試過程中發現是我沒有寫close函數,並且還要寫對位置,寫在用完文件輸入和輸出之後。
最後我添加上了判斷日期是否正確的判斷:
輸入數據的幾種錯誤類型:
1.輸入2010年以前的年份
2.輸入月份超過12月
3.輸入1,3,5,7,8,10,12月時日期超過31
4.輸入4,6,9,11月時日期超過30
5.輸入2月時若是閏年日期超過29,非閏年日期超過28
但因爲在if語句裏我不會嵌套進是否是閏年的2月,所以程序有一個缺陷,不能判斷2月時輸入28到30日的正確與否。
下面是代碼:
package homework1;
/**
* @author Lemon
* @time 2018-8-29
* */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Homework1 {
public static void main (String [] args) {
File f1=new File("D:\\in.txt");
File f2=new File("D:\\out.txt");
PrintWriter pw=null; //用文件寫入數據
Scanner sc=null;
try {
sc = new Scanner(f1);
pw = new PrintWriter(f2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> list=new ArrayList<String>(); //創建一個集合用來接收文件中讀入的數據
while (sc.hasNextLine()) {
list.add(sc.nextLine()); //當有下一行時,給集合中添加一個元素
}
System.out.println(list);
for(String i:list){ //循環定義年月日
//Scanner s =new Scanner (System.in);
//System.out.println("請輸入年份");
int year = Integer.parseInt(i.substring(0,4));
//System.out.println("請輸入月份");
int month =Integer.parseInt(i.substring(4,6));
//System.out.println("請輸入日期");
int day= Integer.parseInt(i.substring(6,8));
//判斷輸入日期是否合法
if (year <2010||month>12||month<1||day<1||day>31||(month==4||month==6||month==9)&&day>30){
System.out.println("您輸入的日期不合法");
}
else{ judge(getAllDays(year, month, day));
pw.println(judge(getAllDays(year, month, day)));
}
}
pw.close();
sc.close();
}
//判斷所輸入年份是否爲閏年
public static boolean runNian(int year){
if(year%4==0 && year%100!=0 || year%400==0){ //判斷閏年的方法:1、能整除4且不能整除100 2、能整除400
return true;
}
return false;
}
//判斷是打漁還是曬網
public static String judge(int days){
int x = days%5;
if (x==0||x==1||x==2) { //將相差的天數除以5取餘,如果相差0,1,2天都是在打漁,3,4則爲曬網
return "打漁";
}
else
{
return "曬網";
}
}
//計算總共有多少天
public static int getAllDays(int year,int month,int day){
int sum=0;
for(int i=2010;i<year;i++){ //判斷指定年份-1到2010年有多少天
if(runNian(year)){
sum+=366;
}
else sum+=365;
}
// 再計算本年內有多少天
sum += getBeforeDays(year,month,day);
return sum;
}
//計算本年內還有多少天的方法
public static int getBeforeDays(int year,int month,int day){
int sum=0;
for(int i=1;i<month;i++){
sum+=getDays(year, i);
}
return sum;
}
//計算每個月有多少天
public static int getDays (int year,int month){
int days=0;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if (runNian(year)){
days=29;
}
else {
days=28;
}
break;
}
return days;
}
}