[英]If the user don't type for 5 sec program exits in JAVA
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(
"Bonjoir lol \n Convertire de Celsius à Fahrenheit tape 1 \n Convertire de Fahrenheit à Celsius tape 2 ");
//si on ecrit rien apres 5 sc lapp doit se fermer
Timer tm =new Timer () ;
tm.schedule(new TimerTask() {
int tim =5;
@Override
public void run() {
tim--;
System.out.println(tim);
if (tim==0) { tm.cancel();
System.exit(0);
}
}
}, 1000, 1000);
int choix;
try {
choix = sc.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Caractere non numerique § erreur ");
choix = sc.nextInt();
}
System.out.println("D'accord");
if (choix != 1 | choix != 2) {
while (choix != 1 && choix != 2) {
System.out.println("Reassayez , tapez soit 1 soit 2 !!!!!");
try {
choix = sc.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Caractere non numerique 1 ou 2");
choix = sc.nextInt();
}
}
}
if (choix == 1) {
System.out.println("Entrez la temperature en Celsius ");
int tempc = sc.nextInt();
System.out.println(celtofar(tempc) + " F°");
} else if (choix == 2) {
System.out.println("Entrez la temperature en Fahrenheit ");
int tempf = sc.nextInt();
System.out.println(fartocel(tempf) + "C°");
sc.close();
}
}
}
Hello , I used a chronometer when Tim=0 ,it exits the program.But how can I make the chronometer reset every time I type something in my keyboard ?? 您好,我在Tim = 0时使用了天文钟,它退出了程序。但是,每次我在键盘上键入内容时,如何使天文钟复位? Hope someone can help and thanks anyway . 希望有人可以帮助和感谢。 (The chronometer is in the first lines) (天文钟在第一行)
Declate tim
in the start of the function, so you can accsess it later and reset it everytime you get an input. 在函数开始时先确定tim
,这样您以后就可以进行注册,并在每次输入时重置它。
public static void main(String[] args) {
int tim = 5;
//all the timer code, but without the int tim = 5
try {
choix = sc.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Caractere non numerique § erreur ");
choix = sc.nextInt();
}
tim = 5; //this line will reset the same variable the clock is using so the clock will reset
while (choix != 1 && choix != 2) {
System.out.println("Reassayez , tapez soit 1 soit 2 !!!!!");
try {
choix = sc.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Caractere non numerique 1 ou 2");
choix = sc.nextInt();
}
tim = 5; //same for all the other attempts to read the input.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.