【发布时间】: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