这是我的程序,它不运行就被终止。在这种情况下,我试图每小时,每分钟和每秒打印一次。
public class hello {
String type;
String name;
static int hour = 0;
static int min;
static int sec;
public static void main(String args[]) {
while(hour<12) {
for(int mins = 0; mins <= 61; mins++) {
min = mins;
if(min==61) {
hour++;
min=0;
continue;
}
for(int secs = 0; secs>=61; secs++) {
sec=secs;
if(sec==61) {
min++;
sec=0;
continue;
}
System.out.println("0"+hour+":"+min+":"+sec);
}
}
}
}
}
当我尝试通过删除第二个for循环并以这种方式编写它来仅打印几分钟和几小时时,它可以正常运行,但是当我添加打印秒数的代码时,它也会失败。
public class hello {
String type;
String name;
static int hour =0;
static int min;
static int sec;
public static void main(String args[]) {
while(hour<12) {
for(int mins=0 ;mins<=61;mins++) {
min = mins;
if(min==61) {
hour++;
min=0;
continue;
}
System.out.println("0"+hour+":"+min+":"+sec);
}
}
}
}
答案 0 :(得分:2)
秒数的条件:
for(int secs = 0;secs>=61;secs++)
是有缺陷的。像这样更改标志:
for(int secs = 0;secs<=61;secs++)
答案 1 :(得分:0)
您的逻辑中有几个错误:
相应地修改if-checks
public class Hello {
static int hour = 0;
static int min;
static int sec;
public static void main(final String args[]) {
while (hour < 12) {
for (int mins = 0; mins <= 60; mins++) {
min = mins;
if (min == 60) {
hour++;
min = 0;
continue;
}
for (int secs = 0; secs <= 60; secs++) {
sec = secs;
if (sec == 60) {
min++;
sec = 0;
continue;
}
System.out.println("0" + hour + ":" + min + ":" + sec );
}
}
}
}
}