英文:
how to make a second counter
问题
我正试图使用do-while循环制作一个秒表,当用户输入等于q时停止。但我对如何使计时器计算秒数毫无头绪,这是我的代码:
public class Search {
static public void main(String args[]) throws IOException {
int seconds = 0;
System.out.println("插入 q 停止:");
char i;
i = (char) System.in.read();
do {
seconds++;
} while(i != 'q');
}
}
输出是这样的:
插入 q 停止:
q
1
英文:
I'm trying to make a second counter using a do-while loop, that stops when the user's input equals q. But I have no idea about how to make the counter counting seconds, this is my code:
public class Search {
static public void main(String args[]) throws IOException {
int seconds = 0;
System.out.println("Insert q to stop:");
char i;
i = (char) System.in.read();
do {
seconds++;
} while(i != 'q');
}
}
And the output is this one:
Insert q to stop:
q
1
答案1
得分: 2
以下行应位于循环内部。
i = (char) System.in.read();
英文:
Following line should be within the loop.
i = (char) System.in.read();
</details>
# 答案2
**得分**: 0
```java
如果你想知道经过了多少时间,可以使用 `System.currentMillis()`。而不是放置一个用于计算秒数的计数器。
```java
long startTime = System.currentMillis();
//... 你的代码
long endTime = System.currentMillis();
long timePassed = endTime - startTime;
英文:
If you want to know how much time passed you can use System.currentMillis()
. Instead of putting a counter for seconds.
long startTime = System.currentMillis();
//... your code
long endTime = System.currentMillis();
long timePassed = endTime - startTime;
答案3
得分: 0
如果您想要获取输入字符“q”的时间(以秒为单位),则以下代码将实现此功能:
public class Search {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
char i;
do {
System.out.print("输入 q 停止:");
String str = sc.next();
i = str.charAt(0);
} while (i != 'q');
long endTime = System.currentTimeMillis();
double executionTime = (endTime - startTime) / 1000d;
System.out.println(executionTime);
}
}
英文:
If you intend to get the time in seconds till q is entered then the following code will do that
public class Search {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Scanner sc = new Scanner(System.in);
char i;
do {
System.out.print("Insert q to stop:");
String str = sc.next();
i = str.charAt(0);
} while (i != 'q');
long endTime = System.currentTimeMillis();
double executionTime = (endTime - startTime) / 1000d;
System.out.println(executionTime);
}
}
答案4
得分: 0
如果您想使用second
变量来跟踪从程序启动到用户输入q
的时间。我建议您将seconds
声明为global
,而不是声明为local
。
public class Search {
int seconds = 0; // 改变
static public void main(String args[]) throws IOException {
System.out.println("插入 q 停止:");
char i;
i = (char) System.in.read();
do {
seconds++;
} while(i != 'q');
}
}
如果您想读取输入是否为 q
,您可以这样做:
public class Search {
static public void main(String args[]) throws IOException {
int seconds = 0;
System.out.println("插入 q 停止:");
char i;
do {
i = (char) System.in.read(); // 改变
seconds++;
} while(i != 'q');
}
}
因为当i = (char) System.in.read()
在do-while
之外时,它只会读取一次,如果输入不是q
,将会导致无限循环。
英文:
If you want to use the second
varibale to keep track of time from when the program started till the time user inputs q
. <br>
I'll suggest instead of declaring seconds
as local
declare it in global
.
public class Search {
int seconds = 0; // changed
static public void main(String args[]) throws IOException {
System.out.println("Insert q to stop:");
char i;
i = (char) System.in.read();
do {
seconds++;
} while(i != 'q');
}
}
And if u want to read the whether q
is input or not you can do:
public class Search {
static public void main(String args[]) throws IOException {
int seconds = 0;
System.out.println("Insert q to stop:");
char i;
do {
i = (char) System.in.read(); // changed
seconds++;
} while(i != 'q');
}
}
Because when i = (char) System.in.read()
is out of do-while
it will only read once and will be an infinite loop if q
wasn't the input.<br>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论