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

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

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

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

  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void main(String[] args) {
  5. Scanner input= new Scanner(System.in)
  6. int cakeNo = 0;
  7. double cake1;
  8. double cake2;
  9. double cake3;
  10. double cake4;
  11. double cake5;
  12. int quantity;
  13. double totalSales = 0;
  14. while(cakeNo !=0 )
  15. System.out.println("Enter cake number 1-5");
  16. cakeNo=input.nextInt();
  17. System.out.println("Enter quantity");
  18. quantity = input.nextInt();
  19. switch (cakeNo){
  20. case 1: cake1 = 3.70;
  21. totalSales+=(3.70*quantity);
  22. break;
  23. case 2: cake2 = 4.20;
  24. totalSales+=(4.20*quantity);
  25. break;
  26. case 3: cake3 = 4.50;
  27. totalSales+=(4.50*quantity);
  28. break;
  29. case 4: cake4 = 5.00;
  30. totalSales+=(5.00*quantity);
  31. break;
  32. case 5: cake5 = 5.50;
  33. totalSales+=(5.50*quantity);
  34. break;
  35. }
  36. System.out.println(totalSales);
  37. }
  38. }

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

t8e9dugd

t8e9dugd1#

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

1u4esq0p

1u4esq0p2#

我会这么做:

  1. public class Main {
  2. LinkedList<Product> cart = new LinkedList<Product> ();
  3. Scanner scanner = new Scanner(System.in);
  4. double tax = 7.5;
  5. boolean done = false;
  6. public Main() {
  7. cart.add(new Product("Cake 1", 3.70));
  8. cart.add(new Product("Cake 2", 4.20));
  9. cart.add(new Product("Cake 3", 4.50));
  10. cart.add(new Product("Cake 4", 5.00));
  11. cart.add(new Product("Cake 5", 5.50));
  12. }
  13. public void displayCart() {
  14. for (int i = 0; i < cart.size(); i++) {
  15. switch (cart.get(i).quantitySelected){
  16. case 0:
  17. System.out.println(i + ": " + cart.get(i).name + " none selected");
  18. break;
  19. default:
  20. System.out.println(i + ": " + cart.get(i).name + " selected: " + cart.get(i).quantitySelected);
  21. break;
  22. }
  23. }
  24. }
  25. public void addToCart(int product, int amount) {
  26. cart.get(product).select(amount);
  27. }
  28. public void getSelection() {
  29. int productSelected;
  30. System.out.println("enter a product value or FINNISHED to end: ");
  31. try {
  32. productSelected = scanner.nextInt();
  33. } catch (InputMismatchException e) {
  34. done = true;
  35. return;
  36. }
  37. System.out.println("enter amount to select: ");
  38. int amount = scanner.nextInt();
  39. cart.get(productSelected).select(amount);
  40. }
  41. public double getSubtotal() {
  42. double cost = 0.00;
  43. for (Product product : cart) {
  44. cost += product.cost * product.quantitySelected;
  45. }
  46. return cost;
  47. }
  48. public double getTotal() {
  49. return getSubtotal() + getSubtotal()*tax;
  50. }
  51. public void finnishPurchase() {
  52. System.out.println("---------------------");
  53. System.out.println("subtotal: " + getSubtotal());
  54. System.out.println("tax: " + tax);
  55. System.out.println("total: " + getTotal());
  56. }
  57. public static void main(String[] args) {
  58. Main store = new Main();
  59. while (!store.done) {
  60. store.displayCart();
  61. store.getSelection();
  62. }
  63. store.finnishPurchase();
  64. }
  65. }
  66. public class Product {
  67. String name;
  68. double cost;
  69. int quantitySelected = 0;
  70. public Product(String name, double cost) {
  71. this.name = name;
  72. this.cost = cost;
  73. }
  74. public void select(int quantity) {
  75. quantitySelected = quantity;
  76. }
  77. }
展开查看全部

相关问题