Java 如何制作第二个计数器
我尝试使用do while循环生成第二个计数器,当用户的输入等于q时,该计数器停止。但我不知道如何使计数器计数秒,这是我的代码:Java 如何制作第二个计数器,java,counter,Java,Counter,我尝试使用do while循环生成第二个计数器,当用户的输入等于q时,该计数器停止。但我不知道如何使计数器计数秒,这是我的代码: public class Search { static public void main(String args[]) throws IOException { int seconds = 0; System.out.println("Insert q to stop:"); char i
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');
}
}
输出是这样的:
Insert q to stop:
q
1
如果您想知道经过了多少时间,可以使用
System.currentMillis()
。而不是放一个计数器几秒钟
long startTime=System.currentMillis();
//... 你的代码
long-endTime=System.currentMillis();
长时间通过=结束时间-开始时间;
如果您想知道可以使用System.currentMillis()
花费多少时间。而不是放一个计数器几秒钟
long startTime=System.currentMillis();
//... 你的代码
long-endTime=System.currentMillis();
长时间通过=结束时间-开始时间;
以下行应该在循环中
i = (char) System.in.read();
下面的行应该在循环中
i = (char) System.in.read();
如果您打算以秒为单位获取输入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("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);
}
}
如果您打算以秒为单位获取输入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("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);
}
}
如果要使用
second
变量跟踪从程序启动到用户输入q
的时间我建议不要将
seconds
声明为local
在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');
}
}
如果您想读取是否输入了q
,您可以执行以下操作:
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');
}
}
因为当
i=(char)System.in.read()
超出do时,
只读取一次,如果q
不是输入,它将是一个无限循环。如果您想使用
秒
变量跟踪从程序启动到用户输入q
的时间我建议不要将
seconds
声明为local
在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');
}
}
如果您想读取是否输入了q
,您可以执行以下操作:
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');
}
}
因为当
i=(char)System.in.read()
退出时,如果q
不是输入,它将只读取一次,并且将是一个无限循环。
问题不仅仅是清楚你想要什么输出不是来自该代码。问题不仅仅是清楚你想要什么输出不是来自该代码。