5 Replies - 309 Views - Last Post: 09 November 2018 - 05:03 PM Rate Topic: -----

#1 Shaw358   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-November 18

Why does my if else statement show me the wrong message?

Posted 08 November 2018 - 03:29 PM

I always end up with "Not enough money!" but i don't know why. Is there something i'm missing here?


String PAYH1 = scan.nextLine();
            if (PAYH1.equals("PAYH1"))
            {
                System.out.println("please enter amount amout of money you wish to desposit (in Euro's).");
                Scanner s = new Scanner(System.in);
                int deposit = s.nextInt();
                int price;
                    price  = 2;
                int sum = deposit - price;
                if (sum > 0)
                {
                    System.out.println("Not enough money!");
                }
                else
                {
                    System.out.println("Thank you for visiting!");
                    System.out.println("You have " + sum + " cent(s) in exchange.");
                }
:code:

This post has been edited by modi123_1: 08 November 2018 - 03:40 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Why does my if else statement show me the wrong message?

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 728
  • View blog
  • Posts: 5,525
  • Joined: 25-December 13

Re: Why does my if else statement show me the wrong message?

Posted 08 November 2018 - 03:44 PM

Quote

I always end up with "Not enough money!" but i don't know why

What is the value in sum when that message is printed?

Print out its value so you can see it. Also print out the values of the variables used to compute the value in sum.
Was This Post Helpful? 1
  • +
  • -

#3 chanjul   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 11-September 18

Re: Why does my if else statement show me the wrong message?

Posted 09 November 2018 - 01:32 AM

in your condition
sum > 0 means that the value in sum is more than zero and you set inside the condition to print
System.out.println("Not enough money!");


the variable deposit is receiving value from the user. so if the value is 2000 and then got into the formula you made which is :
int deposit = s.nextInt();
int price;
price  = 2;
int sum = deposit - price;


the result is 1998 which is more than 0 and then it will print
System.out.println("Not enough money!");



should you just revert the inside it? or just change the condition inside if from
if (sum > 0)
to
if (sum <= 0)

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6645
  • View blog
  • Posts: 27,201
  • Joined: 12-December 12

Re: Why does my if else statement show me the wrong message?

Posted 09 November 2018 - 01:38 AM

Quote

I always end up with "Not enough money!" but i don't know why.


I have this problem constantly.
Was This Post Helpful? 3
  • +
  • -

#5 chanjul   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 11-September 18

Re: Why does my if else statement show me the wrong message?

Posted 09 November 2018 - 01:39 AM

View Postandrewsw, on 09 November 2018 - 01:38 AM, said:

Quote

I always end up with "Not enough money!" but i don't know why.


I have this problem constantly.

haha
same situation too :clap:
Was This Post Helpful? 0
  • +
  • -

#6 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 152
  • View blog
  • Posts: 965
  • Joined: 05-December 13

Re: Why does my if else statement show me the wrong message?

Posted 09 November 2018 - 05:03 PM

Your conditional logic reads (if sum of money is greater than nothing/zero), then you have not enough money. So if the sum is more than zero, your condition is true. Resulting in being broke

PS andrewsw sounds like you need to leave the bank of Shaw358 :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1