2

我有一个正在为编程课做的练习。我遇到了一个看起来非常基本的奇怪错误,但我在调试它时遇到了麻烦。

引用该代码时,会创建标准的 StopWatch 对象,其中包含与其关联的多个实例方法。我在代码底部创建了一个 main 方法,以测试 StopWatch 类中的每个方法,以确保它正常工作。

目前,当我运行程序时,我收到一条错误消息:

Exception in thread "main" java.lang.NoSuchMethodError: main.

我显然在这个类中有一个主要方法,所以我不确定为什么会出现这个错误。

main 方法实现了 Gambler 的毁灭程序进行测试。我目前正在尝试测试 stop() 和 elapsedTime() 方法。完整代码如下:

/* Notes:
 * Start is the date of birth of the object. Most stopwatch don't keep track of when they were 
 * created.
 */

public class Stopwatch {

    public long startTime; //The creation time of the stopwatch
    public long totalTime; //Total time since watch was zeroed
    boolean running = false;      //This will flag if the watch is started or stopped

    public Stopwatch() //First instance method called Stopwatch. What the client will use to create Stopwatch. This serves as the constructor.
    {
            start();
    }

     public void start()
     {
         startTime = System.currentTimeMillis();
         running = true;
     }

     public void stop()
     {
         if(running) {
             totalTime += System.currentTimeMillis() - startTime;
             running = false;
         }

     }

     public double elapsedTime()
     {
         if(running){
             return System.currentTimeMillis() - startTime;
         }
         else{
             return 0; //If the watch isn't currently running, return a 0 value.
         }
     }

     public void zero()
     {
         totalTime = 0;
         start();
     }

    public static void main(String[] args)
    {
        // Run T experiments that start with $stake and terminate on $0 or $goal.

                Stopwatch program_time = new Stopwatch();
                int stake = Integer.parseInt(args[0]);
                int goal = Integer.parseInt(args[1]);
                int T = Integer.parseInt(args[2]);
                int bets = 0;
                int wins = 0;
                for (int t = 0; t < T; t++)
                { 
                    // Run one experiment

                    int cash = stake;
                    while (cash > 0 && cash < goal)
                    {
                        // Simulate one bet.

                        bets = bets + 1;
                        if (Math.random() < 0.5)
                        {
                            cash = cash + 1;
                        }
                        else 
                        {
                            cash = cash - 1;
                        }
                    } // Cash is either going to be at $0 (ruin) or $goal (win)
                if (cash == goal)
                {
                    wins = wins + 1;
                }

            }
                System.out.println(100 * wins / T + "% wins");
                System.out.println("Avg # bets: " + bets/T);
                program_time.stop();
                System.out.println(program_time.elapsedTime());

        }

}

有什么想法吗?

4

1 回答 1

0

检查文件名和类名——它们必须完全匹配,即使是大写和小写。

检查类路径是否正确。

于 2012-11-01T03:04:15.450 回答