如何使扫描器(Scanner)和定时器(Timer)同时运行。

huangapple go评论77阅读模式
英文:

How to have Scanner and Timer run at the same time

问题

if (i == 10) {
    System.out.println("New Passcode:");
    i = -1;
    d = 0;
    passCode.clear();
    Attempt.clear();
    for (int j = 1; j <= 9; j++) {
        Random rand = new Random();
        int upperbound = 10;
        int keycode = rand.nextInt(upperbound);
        passCode.add(keycode);
        x++;
    }
    System.out.println(passCode);

    // This is the password input

    for (int f = 1; f <= 9; f++) {
        d++;
        System.out.println("Input one digit at a time; You are on digit " + d);
        int passAtt = input.nextInt();
        Attempt.add(passAtt);
    }
    System.out.println(Attempt);
} else {
    i++;
    System.out.println("Timer: " + i + " Seconds");
}
while (x >= 1) {
    if (passCode.equals(Attempt)) {
        System.out.println(" ");
        System.out.println("ACCESS GRANTED");
        System.exit(1);
    }
}
}
英文:

I am trying to make a rotating password program, the issue I am running into is when the program asks for the digits of the password, the timer will stop. I think I could separate the program into 2 operational files and a main file, I would not like to go this route though.

Does anyone have any ideas on how I can solve this problem?

'''

if(i == 10)
        {
            System.out.println(&quot;New Passcode: &quot;);
            i = -1;
            d = 0;
            passCode.clear();
            Attempt.clear();
            for(int j = 1; j &lt;= 9;j++)
      {
         Random rand = new Random();
         int upperbound = 10;
         int keycode = rand.nextInt(upperbound);
         passCode.add(keycode);
         x++;
      }
         System.out.println(passCode);
         
         //This is the password input
         
               for(int f = 1; f &lt;= 9;f++)
      {
         d++;
         System.out.println(&quot;Input one digit at a time; You are on digit &quot; + d);
         int passAtt = input.nextInt();
         Attempt.add(passAtt);
      }
      System.out.println(Attempt);
        }
        else{
            i++;
            System.out.println(&quot;Timer: &quot; + i + &quot; Seconds&quot;);
        }
        while(x &gt;= 1)
        {
            if(passCode.equals(Attempt))
        {
            System.out.println(&quot; &quot;);
            System.out.println(&quot;ACCESS GRANTED&quot;);
            System.exit(1);
        }
        }
    }
&#39;&#39;&#39;

答案1

得分: 0

看起来你正在使用一个整数来标记秒数。相反地,创建一个变量来存储开始时间。然后,每当你想要获取经过的时间时,从当前时间中减去那个变量。例如,使用System.currentTimeMillis()

下面是一个简单的类,你可以用它来跟踪经过的时间:

class StopWatch
{
    protected long startTime;
    protected long endTime;
    protected boolean running = false;
    
    public void start()
    {
        startTime = System.currentTimeMillis();
        running = true;
    }
    public void stop()
    {
        endTime = System.currentTimeMillis();
        running = false;
    }
    public double getElapsedSeconds()
    {
        if (running) {
            return (System.currentTimeMillis() - startTime) / 1000.0;
        }
        else  {
            return (endTime - startTime) / 1000.0;
        }
    }
}

以及示例用法:

StopWatch st = new StopWatch();
st.start();
Thread.sleep(1000);  // 模拟耗时操作
System.out.println(st.getElapsedSeconds());
Thread.sleep(5500);  // 另一个耗时操作
st.stop();
System.out.println(st.getElapsedSeconds());

输出将会类似于:

1.0
6.514
英文:

Looks like you are using an integer to mark the seconds. Instead, create a variable to store the start time. Then subtract that from the current time whenever you want to get the elapsed time. For example, use System.currentTimeMillis()

Here's a simple class you can use to keep track of the elapsed time:

class StopWatch
{
	protected long startTime;
	protected long endTime;
	protected boolean running = false;
	
	public void start()
	{
		startTime = System.currentTimeMillis();
		running = true;
	}
	public void stop()
	{
		endTime = System.currentTimeMillis();
		running = false;
	}
	public double getElapsedSeconds()
	{
		if (running) {
			return (System.currentTimeMillis() - startTime) / 1000.0;
		}
		else  {
			return (endTime - startTime) / 1000.0;
		}
	}
}

And example usage:

StopWatch st = new StopWatch();
st.start();
Thread.sleep(1000);  // Simulate long-running operation
System.out.println(st.getElapsedSeconds());
Thread.sleep(5500);  // Another long-running operation
st.stop();
System.out.println(st.getElapsedSeconds());;

And the output will be something like:
<pre>
1.0
6.514
</pre>

huangapple
  • 本文由 发表于 2020年9月16日 04:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63909318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定