Thanks guys, work wonderful. Next question: the user is prompted to enter a number for their desired size of pizza, e.g. if you enter 12, your pizza will be 12 inches and so forth. Right now it look like this:
Code:
int inches;
double cost = 12.99;
double biggerSize = 2;
System.out.println("Pizza Size (inches) Cost");
System.out.println(" 10 $10.99");
System.out.println(" 12 $12.99");
System.out.println(" 14 $14.99");
System.out.println(" 16 $16.99");
System.out.println("What size pizza would you like?");
System.out.print("10, 12, 14, or 16 (enter the number only): ");
inches = keyboard.nextInt();
Right now, the price is 12.99 no matter which number you enter. I'll just copy and paste the question since I don't know how to word it:
1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.
2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the size to 12 and the cost to 12.99.
First, I want to use the scanner class to detect the user's input, but I have no idea how to do that, so if you guys can show this noob how, that would be greatly appreciated. After that, I want it to look like this:
note: input is what the user enters when asked what pizza size they want
if ( input = 10)
{
cost = cost - biggerSize;
}
if ( input = 14)
{
cost = cost + biggerSize * 2;
}
So for every size larger than 12 inches, they are charge 2 extra dollars. Is this viable?
Pro tip, Stackoverflow is your best friend
Storky posted while I was typing
First of all, is keyboard a Scanner? I see it using the nextInt() method which is a part of Scanner.
1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.
Ok, let's start off with a bit of background information. A standard if-else statement has a condition, or group of conditions, that need to be met. If the condition(s) is/are not met, control will be sent to the else statement no matter what, provided it exists.
For an if-else if, you can chain multiple if's together by pairing successive else's with if's. These will be run if and only if the previous statement fails. Of course, the first if will always be checked.
Take this code for example:
Code:
int x = 5;
if(x == 3) {
System.out.println("x equals 3");
}
if(x == 4) {
System.out.println("x equals 4");
}
if(x == 5) {
System.out.println("x equals 5");
}
There are three separate if statements. Therefore, each statement will be checked for validity in the order they appear. In this case, three will be checked, but only one will be true.
A similar situation with if-else if:
Code:
int x = 5;
if(x == 4) {
System.out.println("x equals 4");
}
else if(x == 5) {
System.out.println("x equals 5");
}
else if(x == 6) {
System.out.println("x equals 6");
}
When we add an if to the else statement, we can optionally go to the alternative statement, we aren't forced to go as with a simple else. Here, only two statements will be evaluated with the second statement being true, and the third being skipped altogether. When they are all chained together like this, the chain is broken once one of the statements returns true.
Scanner is simply a class that takes the System input and makes it readable by your program.
Now, we could do something like this for your problem:
Code:
input = keyboard.nextInt(); //will only take an integer, be careful, it may throw an exception for non-integers
if ( input == 10)
{
//do stuff here
}
else if ( input == 12)
{
//do stuff here
}
else if ( input == 14)
{
//do stuff here
}
else if ( input == 16)
{
//do stuff here
}
else
{
//do problem 2 stuff here
}
Notice the last else. As I said before, when an else has no condition, it will always be evaluated if it's previous if condition fails. This is why it's called a "default else".
Recap:
1. We use if-else if when we want to stop when we only have one possible match for our input/data.
2. The default else is used when we want a conditional statement to be run no matter what.