英文:
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("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);
}
}
}
'''
答案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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论