私がやろうとしているのは、この既存のコードに停止関数を追加することです。停止のためにsと入力すると、プログラムが現在の時刻に停止する場所に文字列入力を入力すると、停止関数を実装できると思いました。したがって、sを押すと、ifステートメントなしで実行した場合と同じように動作します。
public class Stopwatch {
private final long t;
public Stopwatch()
{
t=System.currentTimeMillis();
}
public double elapsedTime()
{
return (System.currentTimeMillis() - t) / 1000.0;
}
public double stopping(double newton, double time, double totnewt, double tottime)
{
double timeNewton = newton;
double timeMath = time;
double totalNewton = totnewt;
double totalMath = tottime;
StdOut.println(totalNewton/totalMath);
StdOut.println(timeNewton/timeMath);
return time;
}
public static void main(String[] args)
{
System.out.println("Enter N:");
int N = StdIn.readInt();
double totalMath = 0.0;
Stopwatch swMath = new Stopwatch();
for (int i = 0; i < N; i++)
totalMath += Math.sqrt(i);
double timeMath = swMath.elapsedTime();
double totalNewton = 0.0;
Stopwatch swNewton = new Stopwatch();
for (int i = 0; i < N; i++)
totalNewton += Newton.sqrt(i);
double timeNewton = swNewton.elapsedTime();
String s = StdIn.readString();
if (s == "s")
{
swMath.stopping(timeNewton, timeMath, totalNewton, totalMath);
swNewton.stopping(timeNewton, timeMath, totalNewton, totalMath);
}
StdOut.println(totalNewton/totalMath);
StdOut.println(timeNewton/timeMath);
}
}