Ask coding questions

← Back to all posts
If Statement Question
Rosha18 (2)

Hi everyone! I am new to Java and I have a question about an if statement I made. So, I made an if statement, and basically, here is what I wrote:

if ("Yes"); {
System.out.println("Ok! You will have to pay and extra 10 dollars then.");
else if ("No"); {

Let me explain what I want to get out of this Basically what I am supposed to do is that there is a hotel, and I want to go there. We are supposed to "print" out a receipt at the end with the money that I owe for the hotel. This is a PET hotel and this is where the if statement comes in. The program is supposed to run to ask if I have any pets with me. If I DO have pets, I am supposed to pay ten dollars. So, how am I supposed to add the ten dollars IF I have a pet with me to the receipt?
Thank you!

SixBeeps (5047)

I'm assuming you already have a variable that keeps track of the price on the receipt.

Under the first if-statement, the one that goes off if the user has pets, you can add 10 to that amount, something like:

if (userHasAPet) {
  price += 10;
} else {
  // Don't change the variable in here

Of course, you don't have to make this into a completely new if-statement and just add on to the one you already have.

Also, double check your if-statement for syntax errors, they look a little malformed.

Rosha18 (2)

Great, thank you for the advice! I'm new to Java so I still need to learn some things, so thank you for your help!