Reputation: 4124
I am new to Java and I am practicing doing a java application that loops to ask the user some inputs regarding some items that this person has sold. but I am trying to make my condition of the if statement according to my menu option.. so, if the user inputs something like -1 or any negative number or out of range number this will be grater than 4 the if statement will fail and show the error message that is followed in the else statement. but some how I am missing something that I can't see, so I hope someone of you guys can actually see it and let me know what I am missing..
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
SalesCal a = new SalesCal();
int select = 1; // menu selection
int solds; // sales person input
a.displayMessage();
while(select != 0){
System.out.print("Plese enter \t1 for item1($239.99),\n\t\t2 for item2($129.75),\n\t\t3 for item3($99.95),\n\t\t4 for item4($350.89),\n\t\t0 to cancel: ");
select = input.nextInt(); //getting user input
System.out.print("\n");
if((select <= 4) && (select <= 0) && (select < 0)){
System.out.printf("You selected: item%d\n", select);
System.out.printf("Plese enter the quantity sold for item%d: ", select);
solds = input.nextInt(); //getting user input
System.out.print("\n");
a.GrossCal(select, solds);
} else {
System.err.print("Wrong selectiong, please try again...\n\n");
}
}
a.displayResults();
}
}
Thanks I will really appreciate it your help...
Upvotes: 1
Views: 295
Reputation: 1325
if((select <= 4) && (select >= 0))
, this statement should take care of -ve input
Upvotes: 4
Reputation: 871
Looks like you have one extra handlebar at the very end.
One way to determine which if and else belongs to is find the inner most if. The closest else to this is the else related to the if. You can work outwards from there.
:D
or you could use a switch statement and enter in your own logic if the condition is true:
switch(select) {
case 0: System.out.println("select = 0");
break;
case 1: System.out.println("select = 1");
break;
case 2: System.out.println("select = 2");
break;
case 3: System.out.println("select = 3");
break;
case 4: System.out.println("select = 4");
break;
default: System.out.println("Please enter a number from zero to four");
}
Hope this helps.
Upvotes: 1
Reputation: 20163
if((select <= 4) && (select <= 0) && (select < 0))
A B C
is exactly the same as
if(select < 0)
D
Think about it:
select
is 5: A,B,C and D all fail (result in false
). Both ifs return false
.false
.false
.false
.true
). Both ifs return true
.But if you want less than zero or greater than four to be considered as bad, which is what I think you mean, then you want
if(select < 0 || select > 4)
If zero and four are also bad themselves, then use
if(select <= 0 || select => 4)
or
if(!(select > 0 && select < 4))
Upvotes: 2
Reputation: 234795
I think that the if
statement logic you want is:
if (select > 0 && select <= 4){
System.out.printf("You selected: item%d\n", select);
System.out.printf("Plese enter the quantity sold for item%d: ", select);
solds = input.nextInt(); //getting user input
System.out.print("\n");
a.GrossCal(select, solds);
} else if (select != 0) {
System.err.print("Wrong selectiong, please try again...\n\n");
}
This skips both the processing logic and the error message when the user enters 0.
Upvotes: 2