我制作了一个简单的计算器来计算歌曲的总时间。我使用while函数,但我不知道如何打破它。因此,我不得不永远地继续下去,在每次新的输入之后,它会将总时间写回到这一点。
示例:
12 21 12:21 31 41 44:02
粗体是我输入的内容,非粗体是eclipse写回给我的内容。
我想要的是我输入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 );
}
}
}
}
答案 0 :(得分:0)
在{for -1
输入的for循环检查中:
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 );
}
}
答案 1 :(得分:0)
摆脱变量y
并在循环到while(true)
时将其更改。
用这样的try块环绕整个while循环:
try{
while(true){
...
}
}catch(InputMismatchException e){
/* Just quit */
}
然后指示用户在完成后输入退出,但实际上任何不是int的东西都会退出循环。
答案 2 :(得分: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;
}
}
...
}
答案 3 :(得分: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.");
}
}
答案 4 :(得分: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.");
}
}