2011-03-13 175 views
0

我不确定是否需要在这里粘贴我的代码,但是当我去运行我的代码时,右键单击类对象,它通常表示运行为java应用程序,但现在它所说的只是运行配置。我无法运行java应用程序

我正在使用Eclise。

这是我的代码。我知道这是一个支架安置问题

import java.util.Calendar; 

public class Date { 

    private int month; 
    private int day; 
    private int year; 

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

    public Date(int theMonth, int theDay, int theYear) { 
     month = checkMonth(theMonth); 
     year = checkYear(theYear); 
     day = checkDay(theDay); 
     System.out.printf("Date object constructor for date %s\n", toString()); 
    } 

    private int checkYear(int testYear) { 
     if (testYear > 0) 
      return testYear; 
     else { 
      System.out.printf("Invalid year (%d) set to 1.\n", testYear); 
      return 1; 
     } 
    } 

    private int checkMonth(int testMonth) { 
     if (testMonth > 0 && testMonth <= 12) 
      return testMonth; 
     else { 
      System.out.printf("Invalid month (%d) set to 1.\n", testMonth); 
      return 1; 
     } 
    } 


    private int checkDay(int testDay) { 
     int daysPerMonth[] = 
      { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

     if (testDay > 0 && testDay <= daysPerMonth[ month ]) 
      return testDay; 

     if (month == 2 && testDay == 29 && (year % 400 == 0 || 
      (year % 4 == 0 && year % 100 != 0))) 
      return testDay; 

     System.out.printf("Invalid day (%d) set to 1.\n", testDay); 

     return 1; 
    } 

    public void nextDay() { 
     int testDay = day + 1; 
     if (checkDay(testDay) == testDay) 
      day = testDay; 
     else { 
      day = 1; 
      nextMonth(); 
     } 
    } 

    public void nextMonth() { 
     if (12 == month) 
      year++; 
     month = month % 12 + 1; 
    } 

    public String toString() { 
     return String.format("%d/%d/%d", month, day, year); 
    } 
} 

class DateTest { 
    public static void main(String args[]) { 
     System.out.println("Checking increment"); 
     Date testDate = new Date(03, 13, 2011); 

     for (int counter = 0; counter < 3; counter++) { 
      testDate.nextDay(); 
      System.out.printf("Incremented Date: %s\n", testDate.toString()); 
     } 
    } 
} 
+0

你的代码是否有'public static void main(String [] args)'? – 2011-03-13 18:40:50

+0

不,我正在重新编排代码中的一些内容,并可能已将其删除。我是新来的Java,所以我不知道我删除了什么。你的声明在右上角?后面加括号吗?谢谢 – Mike 2011-03-13 18:44:05

+1

注意:public static void main(String [] args){ } 这是一个空主。 – 2011-03-13 18:59:48

回答

3

几件事情:

  1. 请删除(您Date类中),下面的代码行。

    公共静态无效的主要(字串[] args){

    }

  2. 确保类Date发现里面Date.java和DateTest是内部DateTest.java发现(因为你可以看到,每个班级名称以大写字母开头,班级名称为,确切地说与文件名相同,扩展名为.java)。在DateTest中,您必须相应地导入您的Date课程。

希望这会有所帮助。

+0

当我删除公共静态无效的主要(字符串[] args)我无法运行代码。 – Mike 2011-03-13 22:03:15

+0

这是因为你在同一个文件'Date.java'中有'DateTest'类,即。两个类都在同一个文件中。正如我在第2点提到的那样分开两个班级。 – 2011-03-13 22:06:30

+0

这是我的问题,我不确定如何执行此操作。 – Mike 2011-03-13 22:31:33

0

你改变了你的代码 - 所以我编辑我的答案:

摆脱Date类的主要方法,使您的测试日期类上市。

public class { ... } 

是存储在名为DateClass.java的文件中的DateTest类吗?

1

请记住,为了运行Java应用程序,它必须有一个主要的方法。特别是,它必须声明为:

public static void main(String[] args) 
{ 
    //do stuff here 
} 

当你运行您在Eclipse程序,它实际上做的是执行主所有代码()。它不会执行任何其他方法/代码,除非您从main调用它(或者,当然,它通过main被间接调用)。因此,如果你想要发生任何事情,你需要把你想要的东西放在主体中。例如:

public static void main(String[] args) 
{ 
    System.out.println("Hello World!"); 
} 

当通过Eclipse运行时,将打印出“Hello World!”。并完成。无论你想用Date类做什么,都应该使用该方法。

+0

我试着将括号中的所有内容都括起来,但后来我发现了很多错误。 – Mike 2011-03-13 20:13:27

+0

当然,其他的东西不属于括号内,它们属于Date类。主要方法就是执行其中的任何内容。例如,您可以在主体中创建日期,并打印日期。换句话说,不要改变任何外部的东西!只需将代码放在主体中即可执行您所需的操作。 – donnyton 2011-03-13 20:18:32