我的代码:为什么价格打印两次?
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();
}
}
为什么我的输出打印价格的两倍?
======欢迎来到我的电子商务应用! ======
请输入产品名称:办公桌
真正
价格为:64.43
价格为:85.07
税为:8.51
您的销售总额为:$ 72.94
生成成功(总时间:3秒)
当我从两个用getPrice和getTax除去的System.out.println,这是我的输出
======欢迎到我电子商务应用程序! ======
请输入产品名称:办公桌
真正
税:8.6
你的销售总额为:$ 38.60
没有getter方法打印任何东西。他们应该返回一个值,并没有副作用。 –
我的提交将不会显示价格或税金,但我希望价格仅生成一次,无论如何。 – Sharon
你的代码有太多的问题恕我直言,恕我直言。底线是你打电话两次,这是打印两次。你的许多方法返回你永远不会使用的值。 –