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

时间:2017-07-28 03:01:44

标签: java

            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);

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

1 个答案:

答案 0 :(得分:1)

使用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);