マイコード:価格が2倍に印刷されているのはなぜですか?
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
あなたのコードはあまりにも多くの問題を抱えています。一番下の行は、物事を2度呼び出すことで、2度印刷しています。多くのメソッドは、決して使用しない値を返します。 –