You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

USACO训练提交报错:test类中未找到main方法

Fixing "Main method not found" Error in USACO Java Submission

Hey there, let's get this sorted out! The error you're seeing is because your main method is tucked inside the static inner class InputReader instead of being in the top-level test class. USACO's judge looks for the main method directly in the primary class you submit, so it can't find it where you've put it right now.

Here's the corrected version of your code—notice how the main method is now outside the InputReader class, inside the top-level test class:

import java.util.*; 
import java.io.*; 

class test { 
    static class InputReader { 
        BufferedReader reader; 
        StringTokenizer tokenizer; 

        public InputReader() throws FileNotFoundException{ 
            reader = new BufferedReader(new FileReader("test.in")); 
            tokenizer=null; 
        } 

        String next() { 
            while (tokenizer == null || !tokenizer.hasMoreTokens()){ 
                try{ 
                    tokenizer= new StringTokenizer(reader.readLine()); 
                } catch (IOException e) { 
                    throw new RuntimeException(e); 
                } 
            } 
            return tokenizer.nextToken(); 
        } 

        public int nextInt(){ 
            return Integer.parseInt(next()); 
        } 

        public long nextLong(){ 
            return Long.parseLong(next()); 
        } 

        public double nextDouble(){ 
            return Double.parseDouble(next()); 
        } 
    } 

    // Moved main method here, to the top-level class
    public static void main (String [] args) throws IOException { 
        InputReader r= new InputReader(); 
        PrintWriter pw= new PrintWriter(new BufferedWriter(new FileWriter("test.out"))); 

        int a = r.nextInt(); 
        int b = r.nextInt(); 
        pw.println(a+b); 

        pw.close(); 
    } 
}

Quick Notes:

  • Why this works: The judge expects the entry point (main method) to be in the primary class you submit. By moving it out of the inner class, the system can locate and execute it properly.
  • Naming heads-up: While USACO might accept lowercase class names, it's standard Java practice to capitalize class names (like Test instead of test). It won't break your code, but it's a good habit to build!
  • Local vs Judge behavior: Your IDE probably let you run the inner class's main method directly, which is why it worked locally. The judge doesn't do that—it only looks at the top-level class.

内容的提问来源于stack exchange,提问作者Ivystorm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:47:57