简体   繁体   中英

why is price being printed twice?

My Code:

package ecommerceapp;

import java.util.Scanner;

public class ECommerceApp {
public static void main(String[] args) {
    String productsCatalog = " ";
    //double price = getPrice();
    bannerPrinter();
    productsBuilder();
    boolean exists = getOrder(productsCatalog);

    if (exists == true) {
        double salesTotal = 0;
        printTotal(salesTotal);
    } else {
        System.out.println("The product not found.");
    }
    //double price = 0;
    //double tax = getTax(price);

    //getTotal(price, tax);
}

public static void bannerPrinter() {
    System.out.println("******************************************");
    System.out.println("====== Welcome to my eCommerce app! ======");
    System.out.println("******************************************");
}

public static String productsBuilder() {
    String productsCatalog = "Desk      Table     Pen       ";
    return productsCatalog;
}

public static boolean getOrder(String productsCatalog) {
    String userProduct;
    boolean exists = true;
    Scanner scnr = new Scanner(System.in);
    System.out.print("Please enter a product name: ");
    userProduct = scnr.nextLine();
    if (productsBuilder().toLowerCase().contains(userProduct.toLowerCase())) {
        exists = true;
        System.out.println(exists);
    } else {
        exists = false;
        System.out.println(exists);
    }
    return exists;
}

public static double getPrice() {
    double price = 1 + Math.random() * 99;
    price = Math.round(price * 100.0) / 100.0;
    System.out.println("Price is: " + price);
    return price;
}

public static double getTax(double price) {
    double tax = (0.1 * getPrice());
    tax = Math.round(tax * 100.0) / 100.0;
    System.out.println("Tax is: " + tax);
    return tax;
}

public static double getTotal(double price, double tax) {
    double salesTotal = getPrice() + getTax(price);
    return salesTotal;
}

public static void printTotal(double salesTotal) {
    double price = 0;
    double tax = 0;
    System.out.printf("Your sale total is: $%.2f", getTotal(price, tax));
    System.out.println();
}

}

Why is my output printing the price twice?


====== Welcome to my eCommerce app! ======


Please enter a product name: desk

true

Price is: 64.43

Price is: 85.07

Tax is: 8.51

Your sale total is: $72.94

BUILD SUCCESSFUL (total time: 3 seconds)

When I remove System.out.println from both getPrice and getTax, this is my output


====== Welcome to my eCommerce app! ======


Please enter a product name: desk

true

Tax is: 8.6

Your sale total is: $38.60

because you are printing the result of getPrice. and getPrice itself is printing price so as a result, your program prints price twice. delete the "System.out.println" function in your getPrice block.

因为您在getTax()getTotal()中都getTax()了显示价格的getPrice() getTotal()

When you call getTotal (Inside printTotal) you are calling getPrice which in itself has

System.out.println("Price is: " + price);

and as a result of this when you then call the getTax you are calling the getPrice again

double tax = (0.1 * getPrice());

which then calls the println and that is why it prints twice.

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