私はプログラミングクラスのために取り組んでいる演習があります。非常に基本的なエラーが表示されますが、デバッグに問題があります。
コードが参照されると、いくつかのインスタンス メソッドが関連付けられた標準の StopWatch オブジェクトが作成されます。StopWatch クラスの各メソッドをテストして正しく動作することを確認するために、コードの最後にメイン メソッドを作成しました。
現在、プログラムを実行すると、次のようなエラーが表示されます。
Exception in thread "main" java.lang.NoSuchMethodError: main.
このクラスには明らかにメイン メソッドがあるため、このエラーが発生する理由がわかりません。
main メソッドは、テスト用にギャンブラーの破滅プログラムを実装します。私は現在、stop() および経過時間() メソッドをテストしようとしています。完全なコードを以下に示します。
/* 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());
}
}
何かご意見は?