+ 4

Why does my output keep printing?

I know it's in a for loop but I'm wondering how to stop it. I finished it but with a lot of help. https://code.sololearn.com/c2JyX7EXoPtZ/?ref=app

6th Apr 2018, 3:01 PM
Jaren Dogan
Jaren Dogan - avatar
6 Answers
+ 10
You want your code to print the output just once, right? Use the unlabeled 'break' statement to terminate the loop as soon as the desired String is printed once! if (boxes[x] < 10 || boxes[x] > 20) { System.out.println("The location of the blackbox is at "+ blackboxposition + " and weighs "+ blackboxone + " kg"); break; } For more information, refer: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
6th Apr 2018, 3:18 PM
Dev
Dev - avatar
+ 2
https://code.sololearn.com/WJ9QOJ0fEXTj/?ref=app Here's a reference
7th Apr 2018, 8:06 AM
Vikaash
Vikaash - avatar
+ 1
the code is ok and the loop is limited, but the int array is to big for server processing time limit.Try to reduce the int array max for 73 items.This is the timetout limit.Here is the fixed code to try. https://code.sololearn.com/cpx5eWozF4mQ/?ref=app
6th Apr 2018, 3:13 PM
Highman
Highman - avatar
0
thank you and i appreciate you taking time to help me out, however I'm looking for the output just to be one statement not a continuous repeat.
6th Apr 2018, 3:15 PM
Jaren Dogan
Jaren Dogan - avatar
0
thank you too, I didn't know you could apply break to a for loop. Have a great everyone
6th Apr 2018, 3:21 PM
Jaren Dogan
Jaren Dogan - avatar
0
ok,I did understand the problem properly.The brake is correct solution;)
6th Apr 2018, 3:21 PM
Highman
Highman - avatar