簡體   English   中英

在我的商店項目中,我的方法“ checkout”在停止之前會運行多次,而在直接調用表單時僅應運行一次

[英]In my shop project, my method “checkout” runs several times before stopping when it should only run once when called upon form direct

import java.util.*;

public class Shop {

protected String name;
protected double price;
protected int amount;
protected double discountAmt;
protected double discount;
protected boolean setupComp = false;
protected boolean buyComp = false;


public static void main(String[] args) {
    Shop direct = new Shop();
      direct.direct();

    }

public void direct(){

    println("This program supports 4 functions:");
    println("\t*Setup Shop");
    println("\t*Buy Items");
    println("\t*List of items purchased");
    println("\t*Checkout");
    print("Please choose the function you want: ");


    Scanner input = new Scanner(System.in);
    int func = input.nextInt();
    if (func == 1){
        setup();
    }
    if (func == 2){
        buy();
    }
    if (func == 3){
        listItems();
    }

1.到目前為止,一切似乎運行良好,然后結帳反復運行了幾次。 2.我不知道問題是我如何調用checkout()還是在checkout本身內:

    if (func == 4);{ 
        checkout();
    }
    if (func >= 5){ 
        println("Error: do not know " + func); 
        direct();
    }

}


public Shop(){
    name = "";
    price = 0;
    amount = 0;
}

public Shop[] product;

private static void println(String string) {
    System.out.println(string); 
}
private static void print(String string) {
    System.out.print(string);
}

public void setup(){
    print("Please enter the number of items: ");
    Scanner input = new Scanner(System.in);
    int max = input.nextInt();
    product = new Shop[max];

    for (int i = 0; i < max; i++){
    product[i] = new Shop();
    print("Enter name of product " + i + ": ");
        product[i].setName(name = input.next());
    print("Enter the price of the product: ");
        product[i].setPrice(price = input.nextDouble());
    }
    print("Please enter the amount to qualify for discount: ");
    this.discountAmt = input.nextDouble();
    print("Please enter the discount rate(0.1 for 10%): ");
    this.discount = input.nextDouble();
    setupComp = true;
    direct();
}

public void buy(){
    if (setupComp == false){
        println("Shop is not setup yet!");
        direct();
}
    if (setupComp == true){
        Scanner input = new Scanner(System.in);
    for (int i = 0; i < product.length; i++){
        print("Enter the amount of " + product[i].name + ": ");
        product[i].setAmount(amount = input.nextInt());
        buyComp = true;
        }
    direct();
    }
}
public void listItems(){
    if (setupComp == false){
        println("Shop is not setup yet!");
        direct();
    }
    if (setupComp == true && buyComp == false){
        println("Try again: You have not bought anything");
        direct();
    }
    for (int i = 0; i < product.length; i++){
        if (product[i].amount == 0)
            continue;
        else println(product[i].amount + " count of " + product[i].name + " @ " + product[i].price 
                + " = " +  (product[i].amount * product[i].price));
    }
    direct();
}

public void checkout(){
    if (setupComp == false){
        println("Shop is not setup yet!");
        direct();
    }
    if (setupComp == true && buyComp == false){
        println("Try again: You have not bought anything");
        direct();
    }

    double subtotal = 0;
    double total = 0;
    double disc = 0;
    for (int i = 0; i < product.length; i++){
        subtotal += (product[i].amount * product[i].price);
    }
    if (subtotal >= discountAmt){
        disc =(discount * subtotal);
        total = subtotal - (disc);
    }

3.這些printline語句是重復運行的語句。 我的方法(不是這些println語句)包含在循環中,所以我不知道是什么導致了此問題。

    println("Thank you for coming!");
    println("Sub Total: $" + subtotal);
    println("-Discount: $" + (disc));
    println("total\t: $" + total);

}

public void setName(String name){
    this.name = name;
}

private void setPrice(double price) {
    this.price = price;
}
private void setAmount(int amount) {
    this.amount = amount;
}

}

實際上,if語句是一個空的控制流。 未按照您的預期調用checkout方法。 請刪除第一個分號。

if (func == 4);{ checkout(); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM