我如何在扫描仪中使用Int的值

时间:2022-08-26 22:44:12
            int CMC = 279;
            int BDM = 326;
            int CP = 177;
            int CB =  228;
            int CR = 190;
            int PC = 43;
            int CCS = 24;
            int CE = 26;
            int FM = 20;
            int originalpricebake ;

            Scanner bakeorder1 = new Scanner(System.in);
            int order1;             
            System.out.print("\nEnter Your 1st Item Order:");
            order1 = bakeorder1.nextInt();
            Scanner bakeorder2 = new Scanner(System.in);
            int order2;             
            System.out.print("\nEnter Your 2nd Item Order:");
            order2 = bakeorder2.nextInt();
            Scanner bakeorder3 = new Scanner(System.in);
            int order3;             
            System.out.print("\nEnter Your 3rd Item Order:");
            order3 = bakeorder3.nextInt();
            Scanner bakeorder4 = new Scanner(System.in);
            int order4;             
            System.out.print("\nEnter Your 4th Item Order:");
            order4 = bakeorder4.nextInt();
            Scanner bakeorder5 = new Scanner(System.in);
            int order5;             
            System.out.print("\nEnter Your 5th Item Order:");
            order5 = bakeorder5.nextInt();

            originalpricebake = order1 + order2 + order3 + order4 + order5;
            System.out.print("\nThe Current Price: " + originalpricebake);

how can i use the integer values on the top when i scan for an user input for his order? So that when i got the user input an equation will be made at the bottom of the code

当我扫描订单的用户输入时,如何在顶部使用整数值?因此,当我得到用户输入时,将在代码的底部进行等式

1 个解决方案

#1


1  

Use Array to store the values and access it using loop.

使用Array存储值并使用循环访问它。

    int order;
    int price=0;
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter how many orders: ");
    order = Integer.parseInt(scan.nextLine());

     //Create an integer array to store the input

    int bakeorder[] = new int[order];
    //int originalbakeprice[] = new int[order];
    for(int i=0;i<order;i++)
    {
        System.out.print("Enter the order " + (i+1) + " : ");
            bakeorder[i] = Integer.parseInt(scan.nextLine());
             price +=bakeorder[i];
    }
            System.out.println(price);

#1


1  

Use Array to store the values and access it using loop.

使用Array存储值并使用循环访问它。

    int order;
    int price=0;
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter how many orders: ");
    order = Integer.parseInt(scan.nextLine());

     //Create an integer array to store the input

    int bakeorder[] = new int[order];
    //int originalbakeprice[] = new int[order];
    for(int i=0;i<order;i++)
    {
        System.out.print("Enter the order " + (i+1) + " : ");
            bakeorder[i] = Integer.parseInt(scan.nextLine());
             price +=bakeorder[i];
    }
            System.out.println(price);