1

我做了一个简单的计算器来计算歌曲的总时间。我正在使用 while 函数,但我不知道如何打破它。正因为如此,我不得不让它永远持续下去,并且在我每次新输入之后,它都会写回到目前为止的总时间。

例子:

12 21 12:21 31 41 44:02

粗体字是我输入的内容,非粗体字是日食回信给我的内容。

我想要的是我输入X个数字,当我完成后我按下某个键,例如转义然后它会写回总时间..

这是我的完整代码

import java.util.Scanner;

public class train { 
    public static void main(String[] args) {            
        int songM, songS, totalS=0, totalM=0, y=1;
        Scanner scan = new Scanner(System.in);
        System.out.println("write songs, exit with -1");

        while(y > 0) {
            songM = scan.nextInt();
            songS = scan.nextInt();

            totalM = songM + totalM;
            totalS = songS + totalS;


            if(totalS >59) {
                totalM = totalM + 1;
                totalS = totalS % 60;
            }
            if(totalS<=9) {
                System.out.println(totalM+":0"+totalS );
            } else {
                System.out.println(totalM+":"+totalS );             
            }
        }       
    }
}
4

5 回答 5

0

摆脱变量y并将您的 while 循环更改为while(true).

用这样的 try 块包围整个 while 循环:

try{
  while(true){
   ...
  }
}catch(InputMismatchException e){
 /* Just quit */
}

然后指示用户在完成后键入quit但实际上任何不是 int 的东西都会退出循环。

于 2013-09-14T16:23:54.730 回答
0

在您的 for 循环中检查-1输入:

    System.out.println("write songs, exit with -1");

    while(y > 0) {
        songM = scan.nextInt();
        songS = scan.nextInt();

        if(songM.equals("-1") || songS.equals("-1"))
             break;

        totalM = songM + totalM;
        totalS = songS + totalS;


        if(totalS >59) {
            totalM = totalM + 1;
            totalS = totalS % 60;
        }
        if(totalS<=9) {
            System.out.println(totalM+":0"+totalS );
        } else {
            System.out.println(totalM+":"+totalS );             
        }
    }   
于 2013-09-14T15:57:49.677 回答
0

我建议您使用 do-while 循环和命名约定,这是我的代码

public class Train
{
    public static void main(String[] args)
    {
        int songM, songS, totalS = 0, totalM = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("write songs, exit with < 0");

        do
        {
            songM = scan.nextInt();
            songS = scan.nextInt();
            if (songM >= 0 && songS >= 0)
            {
                totalM += songM;
                totalS += songS;

                if (totalS > 59)
                {
                    totalM = totalM + 1;
                    totalS = totalS % 60;
                }
                if (totalS <= 9)
                {
                    System.out.println(totalM + ":0" + totalS);
                }
                else
                {
                    System.out.println(totalM + ":" + totalS);
                }
            }
            else
            {
                break;
            }
        }
        while (songM >= 0 || songS >= 0);
        System.out.println("End.");
    }
}
于 2013-09-14T16:41:36.323 回答
0

最后一个版本,我继续...

public class Train { 
    public static void main(String[] args) {            
        int totalS=0, totalM=0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Input song lengths in mm:ss, exit with 'quit'.");

        readInput: while(true) {
            String songLengths = scan.nextLine();
            String[] inputs = songLengths.split(" ");
            for(int i=0; i<inputs.length; i++){
                String input = inputs[i].trim();
                int split = input.indexOf(':');
                if(split>=0){
                    try{
                        String songMIn = input.substring(0, split);
                        String songSIn = input.substring(split+1);
                        int songM = 0;
                        /* Allows :30 for 30 seconds. */
                        if(songMIn.length()>0){ 
                            songM = Integer.parseInt(songMIn);
                        }
                        int songS = 0;
                        /* Allows 3: for 3 minutes. */
                        if(songSIn.length()>0){ 
                            songS = Integer.parseInt(songSIn);
                        }
                        /* Don't add times unless both were parsed successfully. */
                        totalM += songM;
                        totalS += songS;
                    } catch(NumberFormatException nfe){
                        System.out.printf("Input %s was not recognized as a valid time.\n", input);
                    }
                } else if(input.equalsIgnoreCase("quit")){
                    break readInput;
                } else {
                  System.out.printf("Input %s was not recognized.\n", input);
                }
            }
        }

        while(totalS > 59) {
            totalM++;
            totalS -= 60;
        }

        scan.close();

        System.out.print(totalM+":");
        System.out.println((totalS<10?"0":"")+totalS );
        System.out.println("Done.");
    }
}
于 2013-09-14T17:20:46.047 回答
0

如果您想强制关键字“退出”退出,另一种方法是执行以下操作:

while(true) {
    try{
        songM = scan.nextInt();
        songS = scan.nextInt();
    } catch (InputMismatchException e){
        if(scan.nextLine().trim().equalsIgnoreCase("quit")){
            System.out.println("Goodbye.");
            break;
        } else {
            System.out.println("Unrecognized input, please retry.");
            continue;
        }
    }
    ...
  }
于 2013-09-14T16:29:36.207 回答