我的代码:
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();
}
}
为什么我的输出将价格打印两次?
<小时/>====== 欢迎使用我的电子商务应用程序! ======
<小时/>请输入产品名称:desk
正确
价格为:64.43
价格为:85.07
税费为:8.51
您的销售总额为:72.94 美元
构建成功(总时间:3秒)
当我从 getPrice 和 getTax 中删除 System.out.println 时,这是我的输出
<小时/>====== 欢迎使用我的电子商务应用程序! ======
<小时/>请输入产品名称:desk
正确
税费为:8.6
您的销售总额为:38.60 美元
最佳答案
因为您正在打印 getPrice 的结果。 getPrice 本身是打印价格,因此您的程序会打印价格两次。删除 getPrice block 中的“System.out.println”函数。
关于java - 为什么价格打印两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128659/