0

Hey Guys.. I understand why my code prints "1 hour" bt how it prints "25 hours" I don't. Would someone please care to explain..

I was trying to use a for loop to print 1 hour through to 24 hours..iam fairly new to Java and I will greatly appreciate all the help..thanks in advance. https://code.sololearn.com/c39OuN3KJa8E/?ref=app

15th Aug 2019, 7:30 AM
Allan And Lifted Hands
Allan And Lifted Hands - avatar
7 Respostas
+ 7
Looks like other solutions are for non-edited code, for problem in current edited code & problem, you can just remove that statement which prints "1 hour" like: for(hours=1;hours<=24;hours++); //if (hours==1){System.out.println("1 hour");} {System .out.println (hours + " hours");} Or for(hours=1;hours<=24;hours++){} //if (hours==1){System.out.println("1 hour");} {System .out.println (hours + " hours");} //you can remove that statement printing "1 hour" which I showed in single-line comment, also there is no need to put statement 'System .out.println (hours + " hours");' in braces {}.
15th Aug 2019, 7:51 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
Your code posts 1 to 24, as you wanted. What's the question?
15th Aug 2019, 7:35 AM
HonFu
HonFu - avatar
+ 1
I ran your code and it outputs: 1 hours 2 hours 3 hours ............. 24 hours It didn't print "25 hours". Have you found a solution that it now works as expected?
15th Aug 2019, 7:40 AM
Ipang
+ 1
Is it your question why prints 25 hours? If this is your question, because loop is considered within the second sentence of the print.
15th Aug 2019, 7:42 AM
Hassan Yousef
Hassan Yousef  - avatar
+ 1
devanille thanks a bunch mate ..solved..why didn't i see this.
15th Aug 2019, 8:35 AM
Allan And Lifted Hands
Allan And Lifted Hands - avatar
0
So sorry guys i edited my code..i did not realize it will reflect here..so sorry bt you can run it again now...my concern i did not want it to output "1 hours"
15th Aug 2019, 7:44 AM
Allan And Lifted Hands
Allan And Lifted Hands - avatar
0
Gaurav Agrawal thanks a lot..i see. bt i wanted instead of outputing "1 hours" i should get "1 hour".
15th Aug 2019, 8:33 AM
Allan And Lifted Hands
Allan And Lifted Hands - avatar