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
8 Antworten
+ 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 {}.
+ 4
Your code posts 1 to 24, as you wanted.
What's the question?
+ 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?
+ 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.
+ 1
devanille thanks a bunch mate ..solved..why didn't i see this.
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"
0
Gaurav Agrawal thanks a lot..i see. bt i wanted instead of outputing "1 hours" i should get "1 hour".