2011-02-23 56 views
-3

我写了一个if语句,它应该根据数据写入不同的输出。它适用于int y = 2000, m = 5, d = 06;,但当int y = 2889, m = 44, d = 16;时它不输出正确的值。如果语句对日期不能正常工作

这是我的代码。有人可以帮助我了解什么是错的。

public class Date1 { 

    private int year = 1; // any year 
    private int month = 1; // 1-12 
    private int day = 1; // 1-31 based on month 

    //method to set the year 
    public void setYear(int y) { 
     if (y <= 0) { 
      System.out.println("That is too early"); 
      year = 1; 
     } 

     if (y > 2011) { 
      System.out.println("That year hasn't happened yet!"); 
      y = 2011; 
     } else { 
      year = y; 
     } 
    } 

    public int setMonth(int theMonth) { 
     if (theMonth > 0 && theMonth <= 12) { // validate month 
      return theMonth; 
     } else { // month is invalid 
      System.out.printf("Invalid month (%d) set to 1.", theMonth); 
      return 1; // maintain object in consistent state 
     } // end else 
    } 

    public int setDay(int theDay) { 
     int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

     // check if day in range for month 
     if (theDay > 0 && theDay <= daysPerMonth[ month ]) { 
      return theDay; 
     } 

     // check for leap year 
     if (month == 2 && theDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) { 
      return theDay; 
     } 

     System.out.printf("Invalid day (%d) set to 1.", theDay); 
     return 1; // maintain object in consistent state 
    } 

    //method to return the year 
    public int getYear() { 
     return year; 
    } 

    //method to return the month 
    public int getMonth(){ 
     return month; 
    } 

    //method to return the day 
    public int getDay(){ 
     return day; 
    } 

    // return a String of the form year/month/day 
    public String toUniversalStringTime() { 
     return String.format("The date using a default constructor %d/%d/%d \n", getYear(), getMonth(), getDay()); 
    } // end toUniversalStringTime 
} 

public class Date1Test { 

    public static void main(String[] args) {   
     int y = 2000, m = 5, d = 06;   

     Date1 d1 = new Date1(); //create a new object 

     System.out.println(d1.toUniversalStringTime()); //call toUniversalStringTime() 

     System.out.printf("The date I created is %d/%d/%d \n", y , m , d); 
    } 
} 
+5

*我做了颂歌* ????? – 2011-02-23 11:11:17

+7

你真的应该使用更多的**描述性标题**。我们已经可以看到它是关于标签中的Java,如果你不需要帮助,你就不会在这里发布吧? – 2011-02-23 11:11:26

+0

+1这个问题的描述方式是“代码”有自己的想法! – zengr 2011-02-23 11:11:51

回答

4

我没有看到任何地方在你的代码,你所呼叫的setDay,setMonth或setYear方法,所以我希望调用toUniversalStringTime总是打印

"The date using a default constructor 111 \n" 

然后以后叫你再次打印使用手动对于Y,M和d

"The date I created is 200056 \n" 

你需要调用D1对象的set方法创建之后,或者在参数传递到构造函数的值设置它们,例如,

d1.setYear(y); 
d1.setMonth(m); 
d1.setDay(d); 

但请注意一些已作出与问候重构你的代码的其他意见,因为已经提到,您的每一个setter方法在他们需要先固定的根本缺陷。

其他一般说明你的代码:

在你setYear方法使用的是y的值更新对象的年份变量,但在第二,如果:

if (y > 2011) { 
    System.out.println("That year hasn't happened yet!"); 
    y = 2011; 
} 

你实际上将y设置为2011而不是year,所以这将不起作用。

出于某种原因,在你的setMonth方法中,你实际上并没有设置月份,但是你只是验证传入的值,即如果值不在1和12之间,则返回1.因此,代码不会' t匹配方法的名称,你应该改变一个或另一个。

您的setDay方法与setMonth相同,因为它实际上并没有设置一天,只是验证它。但更糟的是,对setDay的调用在很大程度上取决于已设置的月份和年份,因为您使用monthyear变量来确定该日是否有效。这意味着setDay只能在setMonth和setYear之后调用,否则您将始终默认检查0001年1月(因为月份和年份默认设置为1)。

+0

“您需要在创建后调用d1对象上的set方法,或者将参数传递给构造函数来设置它们。”我怎样才能做到这一点? – Mugetsu 2011-02-23 11:49:59

+0

@ user630040请参阅我最近编辑的答案。 – DaveJohnston 2011-02-23 12:17:09

1

你的setter方法实际上应该设置一个字段并且什么也不返回(void)。 setYear是可以的,但是你的两个setter方法setMonth和setDay不会设置任何类字段并返回一个int值,这是没有意义的。对你的问题是,哪个字段应该设置月更改,以及哪个字段应该更改?一旦你回答了这个问题,你就可以改变你的方法,使它们更好地工作。请评论,如果任何这没有任何意义。

E.G.您的二传手是像这样:

// assuming you have an int field foo that should be > 0 and <= 100 
public int setFoo(int foo) { 
    if (foo < 0) { 
    return 0; 
    } else if (foo > 100) { 
    return 100; 
    } else { 
    return foo; 
    } 
} 

当应改为更像:

public void setFoo(int foo) { 
    if (foo < 0) { 
    this.foo = 0; 
    } else if (foo > 100) { 
    this.foo = 100; 
    } else { 
    this.foo = foo; 
    } 
} 

看到区别?

+0

我不知道如何使setDay void因为返回1. – Mugetsu 2011-02-23 12:15:18

+0

同样,不要返回任何东西。设置方法仅用于设置。见上面的编辑。 – 2011-02-23 12:37:07

2

你一年二传手是错误的:

//method to set the year 
public void setYear(int y){ 

    if (y <= 0) 
    { 
    System.out.println("That is too early"); 
    year = 1; 
    } 

    if (y > 2011) 
    { 
    System.out.println("That year hasn't happened yet!"); 
    y = 2011; 
    } 
    else //<-- problem, makes the next stamtement only be executed if y <= 2011 
    year = y; 
} 

我猜最后else语句是你的问题,它使,如果是2011年之前的一年才会更新,但我猜是因为声明y = 2011它应限制在2011 - 你需要等什么是去除else

,或者以简单的方式写:

public void setYear(int year) { 
    this.year = Math.max(1,Math.min(2011,year)); 
} 
+0

谢谢。 “您需要在创建后调用d1对象上的set方法,或者将参数传递给构造函数来设置它们。”我怎样才能做到这一点? – Mugetsu 2011-02-23 11:56:54