简体   繁体   中英

asking for user input multiple times using loop

I've been trying to complete a program that translate secondes into time for a school assignment but the teacher has given almost no information about how we are supposed to create loops in our code. So I was wondering if somebody could help me because I can't figure out how to re-ask for the user input after the initial input.

private int x;      //premier saisie nombre de seconde
private int min;    //nombre de minute
private int heure;  //nombre d'heures
private int jour;   //nombew de jours


public algo_secondes()
{ Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("entre nombre de seconde: ");
    x = reader.nextInt(); // Scans the next token of the input as an int.
    //once finished
    while (x > 0) {
        min = x / 60; //division entière
        x = x % 60; //nombre de seconde restante
        heure = min / 60; //division entière
        min = min % 60; //nombre de minutes restante
        jour = heure / 24; //division entière
        heure = heure % 24; //nombre d'heures restante
    reader.close();
    System.out.println(jour + ":" +heure + ":" + min + ":" + x);
    // initialise instance variables
    System.out.println("entre nombre de seconde: ");
    x = reader.nextInt();

} ;
}


}

I've been trying to complete a program that translate secondes into time for a school assignment but the teacher has given almost no information about how we are supposed to create loops in our code. So I was wondering if somebody could help me because I can't figure out how to re-ask for the user input after the initial input.

private int x;      //premier saisie nombre de seconde
private int min;    //nombre de minute
private int heure;  //nombre d'heures
private int jour;   //nombew de jours


public algo_secondes()
{ Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.println("entre nombre de seconde: ");
    x = reader.nextInt(); // Scans the next token of the input as an int.
    //once finished
    while (x > 0) {
        min = x / 60; //division entière
        x = x % 60; //nombre de seconde restante
        heure = min / 60; //division entière
        min = min % 60; //nombre de minutes restante
        jour = heure / 24; //division entière
        heure = heure % 24; //nombre d'heures restante
    reader.close();
    System.out.println(jour + ":" +heure + ":" + min + ":" + x);
    // initialise instance variables
    System.out.println("entre nombre de seconde: ");
    x = reader.nextInt();

} ;
}


}

you are closing the reader using

reader.close();

so cannot use it to read the next line. just remove and put it after the while loop

However, there is a better way of doing this.

Scanner needs to be closed at some point. If nothing fails in the program, this is fine enough. Yet java 7 introduced a try with resources statement, like below:

try (Scanner reader = new Scanner(System.in)) {
    // stuff
}

This tells java to effectively replace that with a try block with a finalise part, in which the thing inside the try with resources will be closed. this will protect the closing of the resource whatever happens, as long as the resource implements the AutoCloseable . See here for more details:

Try-with-resources

The full function you will want will look something like this:

try (Scanner reader = new Scanner(System.in)) {

    int x = 1;

    while (x > 0) {

        System.out.println("entre nombre de seconde: ");
        x = reader.nextInt(); // Scans the next token of the input as an int.
        int min = x / 60; //division entière
        x = x % 60; //nombre de seconde restante
        int heure = min / 60; //division entière
        min = min % 60; //nombre de minutes restante
        int jour = heure / 24; //division entière
        heure = heure % 24; //nombre d'heures restante
        System.out.println(jour + ":" + heure + ":" + min + ":" + x);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM