how do i exit an if statement in java?
how do i exit an if statement in java?
How do I exit an if statement and continue the loop when the if statement is in the loop? I tried break but it didn't work. Continue just made the program to keep asking me to input a number.
import java.io.*;
public class pg48
public static void main()throws IOException
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.println("Type a num: ");
int n = Integer.parseInt(in.readLine());
int pro = 1,i,d;
for (; n != 0; )
d = n%10;
if (d == 0)
break;
pro = pro * d;
n = n/10;
System.out.println("Product: "+pro);
continue;
break;
2 Answers
2
You just need to write the method in this way: public static void main(String args)
If you want to use continue;
, you have to move n = n / 10;
line before if statement.
public static void main(String args)
continue;
n = n / 10;
Thx! I used continue and put the line before the if statement and it worked.
– Super Maniac
Aug 28 at 17:16
YW, Note that "i" variable is never used.
– hadiafifi
Aug 28 at 17:50
simple you have to write Stringargs in main method. once you write it can be work properly.
I tried that just now and it didn't work.
– Super Maniac
Aug 28 at 17:08
Thanks for wanting to contribute. The asker can best tell, but my suspicion is that this is not the problem asked about (you are correct, of course, that
String... commandLineArgs
or similar is missing).– Ole V.V.
Aug 28 at 17:09
String... commandLineArgs
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use
continue;
instead ofbreak;
– ernest_k
Aug 28 at 16:50