-1
public class Gas {

    public static void main(String[] args) {

        double price = Double.parseDouble(args[0]);
        double amount = Double.parseDouble(args[1]);
        Boolean pay;
        double finalCost;
        double cost = (price*amount);
        if(pay = false){
            finalCost = cost;
            cost = cost*0.1;
            finalCost = cost + finalCost;
            System.out.println(finalCost);
        }
        if(pay = true){
            System.out.println(cost);
        }
    }
}

Basically my code determines cost of gas, so if pay = false then a credit card is being used and their is an extra charge of 10%. Whereas if pay = true, then cash is being used. Whenever I put in false, it prints out the value as if it was true. How could I fix this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    In Java, `=` is for assigning a value to a variable, not for comparing two values. You can just write `if (! pay)` to check if it's false, and `if(pay)` to check if it's true. – Dawood ibn Kareem Oct 09 '19 at 00:06
  • 1
    you need to initialise what pay is equal to, currently you define that 'pay' is a Boolean, but you don't actually set it to true or false before entering your if statements – DoubleRainbowZ Oct 09 '19 at 00:07
  • yeah, how is your pay variable set? Looks like it's undefined at the moment. – Kevin Mansel Oct 09 '19 at 00:08
  • @DawoodibnKareem To further this a little bit, `==` is used for comparing two values. So if you wanted to keep the same syntax, you would use `if(pay == true)` – Joshua Burt Oct 09 '19 at 00:08

1 Answers1

1

You need to use ==, or in this case that you have a boolean, just do this:

if(pay) {
   ...
}

And you haven't initialized pay, for instance:

Boolean pay = false;
Villat
  • 1,455
  • 1
  • 16
  • 33