我们被要求为秒表构建一个秒表,其格式为“##:##:###”并更新分钟,秒和毫秒(私有实例变量)因此。例如,"1:21:300"
表示1分21秒300毫秒。在Java中使用parseInt分割字符串
所以我想用string.split()
配对parseInt
更新值。但是,该程序不会编译。我的构造函数根据eclipse具有正确的语法,但是我正在做的事情有问题。我从来没有使用split
或parseInt
,所以我可能会使用这100%的错误。谢谢。
public StopWatch(String startTime){
String [] timeArray = startTime.split(":");
if(timeArray.length == 2){
this.minutes = Integer.parseInt(timeArray[0]);
this.seconds = Integer.parseInt(timeArray[1]);
this.milliseconds = Integer.parseInt(timeArray[2]);
}
else if(timeArray.length == 1){
this.minutes = 0;
this.seconds = Integer.parseInt(timeArray[1]);
this.milliseconds = Integer.parseInt(timeArray[2]);
}
else if(timeArray.length == 0){
this.minutes = 0;
this.seconds = 0;
this.milliseconds = Integer.parseInt(timeArray[2]);
}
else{
this.minutes = 0;
this.seconds = 0;
this.milliseconds = 0;
}
}
P.S. JUnit测试说 “ComparisonFailue:预计0:00:000,但被20:10:008” 试图做的时候:
s = new StopWatch("20:10:8");
assertEquals(s.toString(),"20:10:008");
什么是你的秒表的ToString()?如果你没有定义一个,你就继承了Object中的一个,它不会做你想做的事。 –
或者如果你有一个我认为你可能有领先0的问题 – cerkiewny