0

hi
Got 2 questions.
1.How can i simplify 4 variables all with same condition?
if a,b,c,d <1 System.out.println("Invalid input");?
How to put this in a simple code?I know it can be done individually.

2.if the above condition is false i dont want my code to go further and calculate the input.How and i able to do this.Presently everytime i enter an invalid number ,it still processes the output but also come up with the statement"invalid number".

Thank you

Edited by Valiantangel: n/a

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by DJSAN10
0

write the code as below:

if(a < 1 || b < 1 || c < 1 || d || 1)
{     
     System.out.print("Invalid Input");
     break;
}
1

.if the above condition is false i dont want my code to go further and calculate the input.How and i able to do this.Presently everytime i enter an invalid number ,it still processes the output but also come up with the statement"invalid number".

If you want to straight away exit your program you will have to use exit() inside if statement. If you are in a loop and simply want to get out of the loop then you will have to use break as mentioned by adil bashir

if(a < 1 || b < 1 || c < 1 || d || 1)
{
System.out.print("Invalid Input");
break;
}

You need to decide whether you want to use '&&' or '||' depending on what condition you wan to check. If you are looking for any one of a,b,c,d to be < 1 then above code using or condition is fine . If your condition is true when all a,b,c,d < 1 , then you will have to use and (&&) instead

Edited by DJSAN10: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.