java—我必须使用“switch”语句来完成这个任务(在线商店)我被卡住了

fjaof16o  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(366)

锁定6天。关于这个问题的内容,目前还存在争议。它目前不接受新的答案或互动。

编写一个java程序来模拟一个在线商店。这个程序应该从显示产品及其价格列表开始。至少应提供4种产品。
我的代码如下,它没有字符串,但我需要有蛋糕的名称(显示名称和价格)

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
    Scanner input= new Scanner(System.in)

    int cakeNo = 0;
    double cake1;
    double cake2;
    double cake3;
    double cake4;
    double cake5;
    int quantity;

    double totalSales = 0;

    while(cakeNo !=0 )

     System.out.println("Enter cake number 1-5");
     cakeNo=input.nextInt();

     System.out.println("Enter quantity");
     quantity = input.nextInt();

     switch (cakeNo){

         case 1: cake1 = 3.70;
     totalSales+=(3.70*quantity);
     break;
         case 2: cake2 = 4.20;
     totalSales+=(4.20*quantity);
     break;
         case 3: cake3 = 4.50;
     totalSales+=(4.50*quantity);
     break;
         case 4: cake4 = 5.00;
     totalSales+=(5.00*quantity);
     break;
         case 5: cake5 = 5.50;
     totalSales+=(5.50*quantity);
     break;

     }

     System.out.println(totalSales);  
}  

}

非常感谢你的阅读!如果你有主意,请帮忙。

t8e9dugd

t8e9dugd1#

好吧,在你的代码中有一些错误,你应该首先注意。
第一:前5个字符串定义错误。应该是这样的 String cake1 = "Angel cake" 等等。
第二:字符串和双精度有相同的名字你不能这样做。你需要像这样的东西 String cake1Name = "Name" 以及 double cake1Price = price 这样每个蛋糕都有两个不同的属性。
第三:现在代码甚至没有进入while循环。因为cakeno从0开始,while循环中的条件是 cakeNo != 0 就在第一个循环之前,这个条件将被测试,这将是错误的,这意味着循环代码将不会被执行,并将跳转到程序的末尾。
在这个修复之后,仍然有一个小问题。从用户那里得到输入后,如果所说的输入为0,则表示他想离开,程序仍会向他索要数量。当这个条件为真时,您需要添加/更改一些打破循环的内容。我不想给你代码,但我希望这个答案能帮你好运:)

1u4esq0p

1u4esq0p2#

我会这么做:

public class Main {

LinkedList<Product> cart = new LinkedList<Product> ();
Scanner scanner = new Scanner(System.in);
double tax = 7.5;
boolean done = false;

public Main() {
        cart.add(new Product("Cake 1", 3.70));
        cart.add(new Product("Cake 2", 4.20));
        cart.add(new Product("Cake 3", 4.50));
        cart.add(new Product("Cake 4", 5.00));
        cart.add(new Product("Cake 5", 5.50));
    }

    public void displayCart() {
        for (int i = 0; i < cart.size(); i++) {
            switch (cart.get(i).quantitySelected){
            case 0:
                System.out.println(i + ": " + cart.get(i).name + "   none selected");
                break;
            default:
                System.out.println(i + ": " + cart.get(i).name + "   selected: " + cart.get(i).quantitySelected);
                break;
            }
        }
    }

    public void addToCart(int product, int amount) {
        cart.get(product).select(amount);
    }

    public void getSelection() {

        int productSelected;

        System.out.println("enter a product value or FINNISHED to end: ");

        try {
            productSelected = scanner.nextInt();
        } catch (InputMismatchException e) {
            done = true;

            return;
        }

        System.out.println("enter amount to select: ");

        int amount = scanner.nextInt();

        cart.get(productSelected).select(amount);
    }

    public double getSubtotal() {

        double cost = 0.00;

        for (Product product : cart) {
            cost += product.cost * product.quantitySelected;
        }

        return cost;
    }

    public double getTotal() {
        return getSubtotal() + getSubtotal()*tax;
    }

    public void finnishPurchase() {
        System.out.println("---------------------");
        System.out.println("subtotal: " + getSubtotal());
        System.out.println("tax: " + tax);
        System.out.println("total: " + getTotal());
    }

    public static void main(String[] args) {
        Main store = new Main();

        while (!store.done) {
            store.displayCart();
            store.getSelection();
        }

        store.finnishPurchase();
    }
}
public class Product {
    String name;
    double cost;

    int quantitySelected = 0;

    public Product(String name, double cost) {
        this.name = name;
        this.cost = cost;
    }

    public void select(int quantity) {
        quantitySelected = quantity;
    }
}

相关问题