【问题标题】:Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args)ActivityTime类中找不到main方法,请将main方法定义为:public static void main(String[] args)
【发布时间】:2014-12-10 15:24:59
【问题描述】:

我在 http://www.hackerearth.com/problem/algorithm/roys-life-cycle/ 上测试了我的程序。但是,我总是得到错误:在 ActivityTime 类中找不到主方法,请将主方法定义为:public static void main(String[] args) 这是我的程序

import java.util.Scanner;

/**
* Created by DUY on 10/12/2014.
*/
class EatSleepCode {
    public static void main(String[] args){
        int numberDay = 0;
        int numberMinuteOfDay = 18;

        Scanner input = new Scanner(System.in);
        numberDay = input.nextInt();
        input.nextLine();

        String[] str = new String[numberMinuteOfDay];

        for(int i = 0; i < numberDay; i++){
            str[i] = input.nextLine();
        }

        ActivityTime code = new ActivityTime(numberDay,str,'C');

        code.findLongestTime();
    }
} 

class ActivityTime{
    public int longestTimeOfDay;
    public int longestTimeOfTotal;
    public int numberDay;
    public String[] str;
    public char act;
    public ActivityTime(int numberDay, String[] str, char act){
        this.numberDay = numberDay;
        this.str = str;
        this.act = act;
    }
    public void findLongestTime(){
        int tmp1 = 0, tmp2 = 0;
        for(int i = 0; i < numberDay; i++){
            tmp1 = 0;
            for(int j = 0; j < str.length; j++){
                if(str[i].charAt(j) != act){
                    tmp1 = 0;
                    tmp2 = 0;
                }
                else {
                    tmp1 ++;
                    tmp2 ++;
                }
                if(tmp1 > longestTimeOfDay){
                    longestTimeOfDay = tmp1;
                }
                if(tmp2 > longestTimeOfTotal){
                    longestTimeOfTotal = tmp2;
                }
            }
        }
        System.out.println(longestTimeOfDay + " " + longestTimeOfTotal );
    }
}

你能帮我解决这个错误吗?非常感谢

【问题讨论】:

  • 你运行的是 ActivityTime 类而不是 EatSleepCode 吗?你使用的是什么,IDE 或命令行?
  • 我相信类EatSleepCode需要声明public
  • 尝试让第二个类继承到第一个。
  • @DrewKennedy 主类不必是public。 (我不确定,但我刚刚尝试过,它在默认可见性下运行良好。)
  • @chiastic-security 我也刚刚测试过它,没有它似乎没问题。似乎是一个很好的猜测。 :P

标签: java


【解决方案1】:

你应该把这些类分成两个文件,一个叫EatSleepCode.java,一个叫ActivityTime.java

一旦你这样做了,你就会更清楚你正在运行哪一个作为你的主要课程。是EatSleepCode,其中包含public static void main,所以大概这就是您打算作为主要课程的内容; ActivityTime 没有这样的方法,这就是为什么你不能将它作为你的主类运行。这就是错误的意思。

【讨论】:

  • 感谢您的帮助。但我在hackerearth.com/problem/algorithm/roys-life-cycle 上测试了我的程序。本网站不允许上传两个文件。我只能上传一个文件。你能帮我解决这个问题吗。谢谢
  • @RoboticVn 在这种情况下,您应该将ActivityTime 设为EatSleepCode 的内部类。
  • 非常感谢。我测试成功。谢谢你^^
猜你喜欢
  • 2019-06-15
  • 2013-03-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 2017-03-04
  • 2014-05-13
相关资源
最近更新 更多