从随机生成的数组中打印出单个值

时间:2013-03-03 20:45:42

标签: arrays output

对于这项任务,我们需要让一个人输入人数和年数。然后程序从数组中获取随机数,然后给出一堆不同的信息。到目前为止我设法做到的是:
import java.util.Scanner; import java.util.Random;

public class InternSalary {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner s = new Scanner (System.in);
    Random myRandom = new Random();

    int value = myRandom.nextInt(19000) + 1000;
    int years = 0;
    int people = 0;
    int total = 0;
    int average = 0;



    System.out.println("Enter number of people for which you have salary information");
    people = s.nextInt();
    System.out.println("Enter number of years for which you have salary information");
    years = s.nextInt();



    int [][] salaries = new int [people][years];

    for (int i=0; i<people; i++) {
        for (int j=0; j <years; j++)
            salaries[i][j] = myRandom.nextInt (19000)+1000;
        }

    for (int i = 0; i < people; i++) {
        total = 0;
           for(int j = 0; j < years; j++) {
               total += salaries[i][j];

        }

           System.out.println(total/years);
           System.out.println(total);




    }
}

}
   这给出了每年制作一个人的平均值,然后是他们制作的总数。它仍然没有格式化,但我会稍后再这样做。我无法弄清楚的是如何输出每个人每年制作多少钱,这些数字加在一起以获得一个人的总金额。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

在此部分中,您需要打印出每个薪水。 如果你考虑一下,你就把每年的工资加到总数中。那么为什么不用你需要的任何格式打印出来。

for(int j = 0; j < years; j++) {
           total += salaries[i][j];
           //add this line
           System.out.println(salaries[i][j]);
    }