猿问

循环程序在 Eclipse 中终止

这是我的程序,它在没有运行的情况下被终止。在这个过程中,我试图每小时、每分钟和每秒打印一次。


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);

            }

        }

    }


MMTTMM
浏览 180回答 2
2回答

神不在的星期二

秒的条件:for(int&nbsp;secs&nbsp;=&nbsp;0;secs>=61;secs++)有缺陷。像这样更改符号:for(int&nbsp;secs&nbsp;=&nbsp;0;secs<=61;secs++)

哔哔one

您的逻辑中有几个错误:第一个 for 循环:有缺陷的测试表达式,因为一小时只有 60 分钟,而不是 61第二个 for 循环:有缺陷的测试表达式:一分钟只有 60 秒,你也翻转了比较运算符相应地修改 if 检查public class Hello {&nbsp; &nbsp; static int hour = 0;&nbsp; &nbsp; static int min;&nbsp; &nbsp; static int sec;&nbsp; &nbsp; public static void main(final String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; while (hour < 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int mins = 0; mins <= 60; mins++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = mins;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (min == 60) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hour++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int secs = 0; secs <= 60; secs++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sec = secs;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sec == 60) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sec = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("0" + hour + ":" + min + ":" + sec&nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答