-2

My clock code works at every other hour except ten o' clock. At every other hour, it increments minutes by 1 every time seconds is 60, but at ten o' clock, for some reason, it increments minutes by 1 every time seconds is 10. I don't know what I did wrong. Please help!

package misk;


public class Misk {
   public static void main(String[] args) throws InterruptedException {
      int x = 0;
      int sec = 0, min = 0, hour = 9;
      while (x == 0) {
         Thread.sleep(10);
         sec++;
         if (sec == 60) {
            sec = 0;
            min++;
         }
         if (min == 60) {
            min = 0;
            hour++;
         }
         if (sec < 10) {
            if (min < 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":0" + min + ":0" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min < 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":0" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min > 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":0" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min < 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":0" + min + ":0" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min > 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min > 10) {
               if (hour > 10) {
                  System.out.println("0" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min < 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":0" + min + ":" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min > 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min == 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min == 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min > 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min == 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min > 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min > 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min == 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min == 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min < 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":0" + min + ":" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min == 10) {
               if (hour < 10) {
                  System.out.println("" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min < 10) {
               if (hour == 10) {
                  System.out.println("" + hour + ":0" + min + ":0" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min < 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":0" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min == 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":0" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min < 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":0" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min == 10) {
               if (hour > 10) {
                  System.out.println("" + hour + ":" + min + ":0" + sec);
               }
            }
         }
         if (sec > 10) {
            if (min == 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":" + sec);
               }
            }
         }
         if (sec < 10) {
            if (min == 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":0" + sec);
               }
            }
         }
         if (sec == 10) {
            if (min > 10) {
               if (hour < 10) {
                  System.out.println("0" + hour + ":" + min + ":" + sec);
               }
            }
         }
      }
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
nmelssx
  • 100
  • 1
  • 12
  • Please look up and try to follow Java code formatting rules. By following these rules, others will more easily be able to read and understand your code, and then be able to help you. If you are using most IDE's they can help you format your code correctly for you. – Hovercraft Full Of Eels Aug 31 '14 at 15:41
  • I've tried to fix your code formatting for you. There seems to be an awful lot of unnecessary repetition in your code that makes it hard to see what is going on at a glance. – Hovercraft Full Of Eels Aug 31 '14 at 15:43
  • Thank you. Do you know what I did wrong in my codes? – nmelssx Aug 31 '14 at 15:44
  • 4
    I'm having trouble reading your code. I think that you need only one println statement in there, and learn to use `String.format(...)`. Doing this will shrink your code more than 10 fold. For example please see my code in my answer [here](http://stackoverflow.com/a/25592738/522444). – Hovercraft Full Of Eels Aug 31 '14 at 15:45
  • It's overcomplicated. You keep repeating if statements that already exist there. Instead of typing `if(sec < 10)` several times, you should include all of them in the single block (so there's a single `if(sec < 10)` at the outermost level). There's also something called `if-else`, which you might want to research. – Kayaman Aug 31 '14 at 15:46
  • Please take a look at [java.util.Formatter](http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html) and consider using it or something similar. Your code is beyond fixing, though it does give you an opportunity to learn how to use the debugger in your IDE. Using the debugger you'll be able to step through the code and examine the values of variables at any time. – Paul Aug 31 '14 at 15:47
  • I would use a for loop and simply increment one millisecond variable, and then use the `%` and `/` operators inside the loop to figure out hours, min, seconds and milliseconds, similar to what I've done in [this answer of mine](http://stackoverflow.com/a/25592738/522444). Then use `String.format(...)` to display the result. Keep it simple. – Hovercraft Full Of Eels Aug 31 '14 at 15:48
  • 1
    Have you tried the _greater than or equal_ `>=` operator? It won't make the algorithm less awful, but it'll be a lot simpler to find a bug among 8 combinations than among 27 combinations. (By the way, you're only testing 26 combinations in your code, the lack of the 27th is the cause of your bug) – Volune Aug 31 '14 at 16:04

1 Answers1

3

First of all, your code is a mess.

You need to learn how to use conditions more effectively, also learn about formatting.

You can easily add '0' before any digit if it isn't a '2 digit number'.

System.out.println(String.format("%02d %02d %02d", hour, minute, second));

Your code doesn't work at ten o' clock because all of your conditions are 'hour > 10' or 'hour < 10', and both of those are false when hour == 10.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Dávid Szabó
  • 2,235
  • 2
  • 14
  • 26