如何加总成本的篮子?java初学者

gfttwv5a  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(314)
import java.util.Scanner; 

class Main {

  public static void main(String[] args) {

    System.out.println("Welcome to Mercadona");
    System.out.println("-----Mercadona Menu----");
    System.out.println("| 1. Milk £1          |");
    System.out.println("| 2. Eggs £2.50       |");
    System.out.println("| 3. Bread £1.20      |");
    System.out.println("| 4. Sugar £2         |");
    System.out.println("| 5. Toothpaste £3.50 |");
    System.out.println("| 6. Toothbrush £10   |");
    System.out.println("| 7. Birthday Cake £50|");
    System.out.println("-----------------------");

    System.out.println("Minimum order of 3 products");

    System.out.println("Maximum order of 4 products");

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter product");

    String product=input.nextLine();

    System.out.println("Enter quantity");

    int quantity=input.nextInt();  

    Scanner input2 = new Scanner(System.in); 

    System.out.println("Enter product");

    String product2=input2.nextLine();

    System.out.println("Enter quantity");

    int quantity2=input2.nextInt();  

    Scanner input3 = new Scanner(System.in); 

    System.out.println("Enter product");

    String product3=input3.nextLine();

    System.out.println("Enter quantity");

    int quantity3=input3.nextInt();  

    System.out.println("You have reached minimum order limit.");

    System.out.println(" Would you like to continue ordering?");

    System.out.println("Choose number 90 for yes and number 100 for no");

   Scanner input4 = new Scanner(System.in); 

   System.out.println("Enter number");

   int y = input4.nextInt();

   System.out.println(y);

   if (y == 90 ) {

   System.out.println("You can continue shopping.");

   Scanner input5 = new Scanner(System.in); 

   System.out.println("Enter product");

   String product5=input5.nextLine();

   System.out.println("Enter quantity");

   int quantity5=input5.nextInt();  

   } else if (y == 100) {

   System.out.println("Your order basket is complete");

   //now to calculate cost of order basket//
1cklez4t

1cklez4t1#

我能想到的最简单的解决方案是,在不添加数组或任何东西的情况下,使用一个名为double的basket,它只有一个运行总数。在每个项目之后,执行以下操作:

if (product.equalsIgnoreCase("milk")) basket += 1*quantity;
else if (product.equalsIgnoreCase("eggs")) basket += 2.5*quantity;
//add else ifs for each product, and the basket just does += price*quantity for each product

另外,请注意,如果您不使用这么多扫描仪,代码将运行得更加顺畅。计算机实际上只能处理system.in的一个示例,而且扫描器可以无限地使用,直到您键入 input.close() ,所以继续使用输入。
提高效率的另一种方法是一直使用相同的产品和数量变量。因为您总是在更新这个篮子,所以您只需更改相同的变量就可以避免阻塞内存。
如果有用的话就告诉我!

cidc1ykv

cidc1ykv2#

你也许可以这样做。

public class main {

      public static void main(String[] args) {

            System.out.println("Welcome to Mercadona");
            System.out.println("-----Mercadona Menu----");
            System.out.println("| 1. Milk £1          |");
            System.out.println("| 2. Eggs £2.50       |");
            System.out.println("| 3. Bread £1.20      |");
            System.out.println("| 4. Sugar £2         |");
            System.out.println("| 5. Toothpaste £3.50 |");
            System.out.println("| 6. Toothbrush £10   |");
            System.out.println("| 7. Birthday Cake £50|");
            System.out.println("-----------------------");

            System.out.println("Minimum order of 3 products");

            System.out.println("Maximum order of 4 products");

            Scanner input = new Scanner(System.in); 

            System.out.println("Enter product");

            String product=input.nextLine();

            System.out.println("Enter quantity");

            int quantity=input.nextInt();  

            Scanner input2 = new Scanner(System.in); 

            System.out.println("Enter product");

            String product2=input2.nextLine();

            System.out.println("Enter quantity");

            int quantity2=input2.nextInt();  

            Scanner input3 = new Scanner(System.in); 

            System.out.println("Enter product");

            String product3=input3.nextLine();

            System.out.println("Enter quantity");

            int quantity3=input3.nextInt();  

            System.out.println("You have reached minimum order limit.");

            System.out.println(" Would you like to continue ordering?");

            System.out.println("Choose number 90 for yes and number 100 for no");

           Scanner input4 = new Scanner(System.in); 

           System.out.println("Enter number");

           int y = input4.nextInt();

           System.out.println(y);

           int quantity5 = 0;

           if (y == 90 ) {

           System.out.println("You can continue shopping.");

           Scanner input5 = new Scanner(System.in); 

           System.out.println("Enter product");

           String product5=input5.nextLine();

           System.out.println("Enter quantity");

           quantity5=input5.nextInt();  

           } else if (y == 100) {

           System.out.println("Your order basket is complete");
        // TODO Auto-generated method stub

           int total_quantity = quantity + quantity2 + quantity3;

           int total_quantity_after_number_90 = 0;

           if( y == 90 )
           total_quantity_after_number_90 = total_quantity + quantity5;

    }

  }

}

相关问题