Home > Back-end >  Program repeats user input even when an equivalency condition / the first if-statement has been met
Program repeats user input even when an equivalency condition / the first if-statement has been met

Time:10-21

I need an instance in my program to work in that if the user inputs a payment number (payment) that is equal to the total price of a checkout with taxation (price3), the program will just list 0.00 as change and not repeat user input as if the user's payment is less than the price. However, when payment equals price3 (payment - price3 == 0), the program goes to the else-if statement. How do I fix this?

Example: price3 == 28, payment == 28, the output after payment input is "You still owe..." and so on instead of "Your change is $0.00".

I think it is skipping the first if-statement in the while loop, but I have no idea why. I already tried moving around the if-statements in the while-loop to no avail.

There are no error messages.

Note: I am still trying to learn Java. Just started recently.

The program code which my question references is displayed below:

import java.util.Scanner;
public class Checkout
{
   public static void main(String [] args)
  {
     Scanner input = new Scanner(System.in);
     System.out.println("How many items are you purchasing?");
     int count = input.nextInt();
     double price1 = 0;
     double price2 = 0;
     String userItem = "";
     for(int i = 1; i<=count; i  )
     {
        System.out.println("Please enter the name of the item:");
        input.nextLine();
        userItem = input.nextLine();
        System.out.println("Please enter the price of the item:");
        price1 = input.nextDouble();
        System.out.println();
        System.out.printf("Your item #"   i   " is "   userItem   " with a price of $"   "%.2f", price1);
        System.out.println();
        price2 = price2   price1;
     }
     System.out.println();
     System.out.printf("Your total amount is: $"   "%.2f", price2);
     double price3 = price2   (price2 * 0.06);
     System.out.println();
     System.out.printf("Your total amount with tax is: $"   "%.2f", price3);
     System.out.println();
     System.out.println("I need a payment!");
     double payment = input.nextDouble();
     boolean condition = false;
     while(condition == false)
     {
        if(payment - price3 == 0)
        {
           condition = true;
        }
        else if(payment < price3)
        {
           System.out.println();
           System.out.printf("You still owe "   "%.2f", (price3-payment));
           System.out.println();
           System.out.println("I need a better payment!");
           payment = input.nextDouble();
        }
        else
        {
           condition = true;
        }
     }
     double change = payment - price3;
     System.out.println();
     System.out.printf("Your change is: $"   "%.2f", change);
   }
}

CodePudding user response:

The core of your problem lies in (1) expecting exact equality of floating-point value, and (2) displaying quantities to 2 decimal places.

Given the user is told the amount to pay (price3) using 2 places of decimals, even if he enters that exact same value as payment, it may not match the true value of price3.

Ideally you should do all calculation in pennies (or whatever the smaller unit of this currency is). Failing that, your criterion for having paid the right amount should be something like the difference between price and payment is less than 0.01.

In the stated case

Example: price3 == 28, payment == 28, the output after payment input is "You still owe..." and so on instead of "Your change is $0.00".

if the price before tax is 26.415 it makes the price after tax 27.9999, which displays as 28.00 but is not equal to 28.00. Neither 26.41 nor 26.42 get you to an after-tax displayed price of 28.00.

CodePudding user response:

that is happening because of price3=price2 (price2*0.06). So, when it is comparing payment with price3, it is always less. See below

enter image description here

  •  Tags:  
  • java
  • Related