简体   繁体   English

使用循环多次请求用户输入

[英]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只需将其删除并放在 while 循环之后

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:然而,java 7 引入了 try with resources 语句,如下所示:

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.这告诉 java 用带有 finalize 部分的 try 块有效地替换它,其中 try with resources 中的东西将被关闭。 this will protect the closing of the resource whatever happens, as long as the resource implements the AutoCloseable .只要资源实现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);

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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