获取循环的所有输入值并显示它

时间:2015-03-18 14:53:52

标签: java

我想从输入的销售金额中获取所有输入值,但它只给出我输入的最后一个值,并且还显示所有金额。感谢

这是输出的样子。

  

请输入要处理的销售人员数量:3

     

输入和ID号和销售额按空格分隔:1 150

     

输入和ID号和销售额按空格分隔:2 250

     

输入和ID号和销售额按空格分隔:3 350

          Weekly Sales by Salesperson 0         1          2  .
     

此处应显示3个销售额。

     

总销售额:1800

{
    int i = 0;

    System.out.print("Please enter the number of salespeople to be processed: ");
    howmany = input.nextInt();

     int[] array = new int[howmany];


    while ( i <= array.length - 1  )

    {
        System.out.print("Enter and ID number and Sales Amount seperated by a space: ");        
        numberOfSales = input.nextInt();
        Salesmoney = input.nextInt();
        addwan += Salesmoney;
        i++;
    }

    HAHA();

    for (int j = 0; j < i ; j++)
    {

        System.out.print( + j + "     ");

        addwan += Salesmoney;

        System.out.print(Salesmoney + "     ");
    }

    System.out.println("\nTotal Sales: " + addwan + " ");

    }   

public static void HAHA()
{

    System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");

}

}

2 个答案:

答案 0 :(得分:0)

粗略地说,我想你会想要这样的smthg:

System.out.print("Please enter the number of salespeople to be processed: ");
howmany = input.nextInt();

int[][] array = new int[2][howmany];//2D array to store the amount, and worth of the item


for (int i=0; i<howmany;i++  )
{
    System.out.print("Enter and ID number and Sales Amount seperated by a space: ");        
    array[0][i] = input.nextInt();
    array[1][i] = input.nextInt();
}

System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");
int addwan=0;
for (int j = 0; j < howmany ; j++)
{

    System.out.println( "Eintrag: "+j+" number of sales: "+array[0][j]+" itemworth: "+array[1][j]);
    addwan+=array[0][j]*array[1][j];
}

System.out.println("\nTotal Sales: " + addwan + " ");

猜对了,你的身份证号码必须与金额相乘?因为我想知道1800年的销售情况......即使你是1700年......

答案 1 :(得分:0)

import java.util.Scanner;

public class Exchange {
    public static void main(String[] args){


        int howmany;
        int totalAmount;
        int[] arrayForId;
        int[] arrayForSales;

        Scanner inputData = new Scanner(System.in);

        totalAmount = 0;


        System.out.print("Please enter the number of salespeople to be processed: ");
        howmany = inputData.nextInt();

        arrayForId = new int[howmany];
        arrayForSales = new int[howmany];

        for(int i = 0; i < arrayForId.length; i++){
            System.out.print("Enter an ID number and Sales Amount seperated by a space: ");        
            arrayForId[i] = inputData.nextInt();
            arrayForSales[i] = inputData.nextInt();
        }

        System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");

        for (int i = 0; i < arrayForId.length ; i++){

            System.out.println("Id: " + arrayForId[i] + "\t Amount: " + arrayForSales[i]);
            totalAmount += arrayForSales[i];
        }

        System.out.println("\nTotal Sales: " + totalAmount);
        inputData.close();
}

}

希望这会对你有所帮助。