简体   繁体   English

如果用户未输入5秒钟,则程序将在JAVA中退出

[英]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.

相关问题 编写一个Java程序,查询用户一系列单词。 当用户输入“退出”时程序退出 - Write a java program that queries the user for a series of words. The program exits when the user enters 'quit' 处理Java程序异常退出 - Handling abnormal Java program exits 如何将dropbox的auth令牌直接导入java程序,以便用户不需要复制粘贴它 - How to get the auth token for dropbox directly into java program so that user don't need to copy paste it Java 程序让用户输入一个名称列表,然后“ZZZ”停止。 数名字,但不要数“ZZZ” - Java program for user to input a list of names and then "ZZZ" to stop. Count names, but don't count "ZZZ" 不明白java程序的结果 - Don't understand result of java program 不了解简单的Java程序的输出 - Don't understand output of simple java program 关闭 window - 但不要停止程序 - JAVA - Close window - but don't stop program - JAVA 如何配置不等待用户输入运行并快速退出的程序 - How to profile a program that doesn't wait for user input to run and exits quickly Java程序无法完成方法-for循环后退出,没有错误 - Java program won't finish method - exits after a for loop with no errors instead 在没有GUI的情况下在退出的Java程序中设置热键 - Setting a hotkey in a Java program that exits, without a GUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM