Help! NZEC!

Hello! I need some help! I just started doing the Practice-Easy problems, but I’m having a little trouble that I don’t know how to solve.

I asked a friend and he told me that when a problem doesn’t tell when to stop executing you should use the while(true) thing but I’m using it with the problelm = HS08TEST and I got NZEC.

Is there other way to know how to stop? when do I use the while(true) stuff and when should I use another solution that I don’t know yet?

Thanks for your help!

post your code

Ok! I know it is not efficient nor well written, but I’m just starting at this.

public static void main (String[] args) throws java.lang.Exception
{
        Scanner in = new Scanner(System.in);
         int retiro;
         float cuenta;
while(true) {       
         retiro = in.nextInt();
         cuenta = in.nextFloat();
        
        if(retiro%5 == 0 ){ 
            if(retiro > cuenta) {System.out.println(cuenta);}
            else{System.out.println(cuenta-(retiro+0.50));}
            
        }
        else {System.out.println(cuenta);}
    
}
}

Your c/c++ program should return zero while exiting.
Where as in Java it occurs if u throw an exception.

For the particular problem that you are trying to solve (HS08TEST), there is only one test case.
You don’t need any loops there. Just get rid of the while statement, and move everything out of its body.

For the general case, the input either contains the number of test cases at the top, followed by test case data, or it just contains test case data, and you are supposed to process until EOF.

For the second case, the format of input per test case would be well defined. For such cases you can check whether there are any more tokens available in the scanner, at the start of the loop. For example,

while (in.hasNext()) {
    // do something. may be in.nextInt();
}

You can also use while (true), but that will lead to infinite loop. At some point, the program will try to read data that is not available, and will throw some runtime exception (which is what is happening in your case.) You can catch that exception using a try…catch… block, and use that to break out the while loop. But, I would say, that is a very very very very very bad practice. You should not be relying on exceptions to know when to break out of a loop. You should be using other available options.

Again, for the particular problem, you could use the general strategy. But, since the problem has only one test case, you can also get away with just writing the logic directly, and without any loops.

For problems that require you to iterate, i am sure, it would state somewhere that either there are multiple test cases and/or that you should read until EOF, and such.

And, of course, if the first line is the number of test cases, you don’t need to worry about this at all.

Happy programming!

No need to use while, just use IF and ELSE so

if(retiro%5 == 0 && retiro < cuenta) you’ll print: cuenta - (retiro + 0.5) else: print cuenta

Theres a lot of conditions in the problem that are never used, like cuenta <= 200 or that both inputs are supposed to be > 0
Still a good practice, I learned about locale and about decimal formatting

Thanks! I’m gonna try it! It would be cool if you have an example with C/c++ so I can look at it though!

http://www.codechef.com/viewsolution/2390260 This is the solution which got NZEC for me when I was a newbie. It is not returning anything.

And this is the solution which worked for me as it is returning zero upon exit.
http://www.codechef.com/viewsolution/2390260