简体   繁体   中英

Java Homework (beginner) - I think there is something wrong with my math or declarations?

For my CS1 class, we have to write a program in Java that asks the user for input on the following; -restaurant bill amount -how much they would like to tip, given 3 options

The program is then to calculate the original bill amount, the tip, the tax, and the final bill amount-- printed out on the screen.

I am able to get all of this to prompt and print, but the math is off... Can someone please advise? This is new for me so I'm not sure what it is exactly that I'm missing or am off on. Have tried many different things and this is where I am at now. Thank you!

I've tried adding and deleting parenthesis, changing the way things are declared, looking for syntax errors

/*Description: This program calculates a restaurant bill.
 */

import java.util.Scanner;

public class MiniProject3 {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("What is the check amount? ");
        double checkAmount = keyboard.nextDouble();
        System.out.print("What type of tip would you like to leave? ");
        System.out.print("Enter 1 for good tip (20%) ");
        System.out.print("Enter 2 for average tip (15%) ");
        System.out.print("Enter 3 for poor tip (10%) ");
        int tipChoice = keyboard.nextInt();
        double grandTotal;
        double taxTotal = (checkAmount * 0.07);
        double subTotal = (checkAmount + taxTotal);
        if (tipChoice == 1)
            grandTotal = 0.20 * subTotal;
        if (tipChoice == 2)
            grandTotal = 0.15 * subTotal;
        else
            grandTotal = 0.10 * subTotal;
        System.out.print("total check: $" + checkAmount);
        System.out.println();
        System.out.print("tax: $" + taxTotal);
        System.out.println();
        System.out.print("tip: $" + (grandTotal - subTotal));
        System.out.println();
        System.out.print("final bill: $" + grandTotal);
    }
}

No error messages right now-- but math is way off.

There are two ways to solve this problem :

  1. Save calculated tip choice in the other variable and then add that variable to the subtotal to get the grand total.

  2. Just multiply with 1.20 for 20% instead of 0.20 likewise for all the tips to directly get the grand total.

Use below lines of code in your program

if (tipChoice == 1)
        grandTotal = subTotal + 0.20 * subTotal;
if (tipChoice == 2)
        grandTotal = subTotal + 0.15 * subTotal;
else
        grandTotal = subTotal + 0.10 * subTotal;
if (tipChoice == 1)

    grandTotal = 0.20 * subTotal; // grandTotal -> totalTip maybe???

System.out.print("tip: $" + (grandTotal - subTotal));

System.out.println();

System.out.print("final bill: $" +grandTotal); //grandTotal - totalTip

If you took choice 1 then grandTotal would be the total amount of tip you had to pay based on your calculation 0.20 * subTotal (20% of subTotal). This means the final bill would print the total tip amount not the final bill amount. I suggest you check your naming and calcuations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM