【问题标题】:Keep getting complimation errors in array [closed]不断在数组中出现编译错误[关闭]
【发布时间】:2015-01-21 16:25:22
【问题描述】:

假设 Acme Corporation 在德克萨斯州的六个不同地点拥有 Anvil 建筑业务 - 休斯顿(行索引 0)、达拉斯(行索引 1)、亨茨维尔(行索引 2)、圣安东尼奥(行索引 3)、韦科(行索引 4)和 Humble(行索引 5)。在每个地点,他们制造 8 种不同的 Anvil 模型。他们收集了这 8 个模型在 2014 年在其 6 个不同地点的每一个的“销售”数据,并将其保存在一个数组中。让我们首先使用以下语句创建一个名为 sales 的数组:

int[][] sales = new int[6][8];

现在让我们用随机数填充数组 sales。生成 0 到 25 之间的随机数并将其存储在数组中。

现在,完成以下任务:

一个。编写代码来计算和打印每个位置销售的铁砧总数。
湾。 Acme 想确定他们最不受欢迎的砧模型。假设一个模型只在 3 个或更少的城市销售时不受欢迎,编写代码打印所有不受欢迎的模型的列表。 C。哪个地方的员工最勤奋。也就是说,哪个位置卖的铁砧最多。 d。哪个位置售出的铁砧最少。

这是我的代码:

public static void main(String[]args){

int[][] sales = new int[6][8];



int sum=0;
for (int row=0; row<6;row++){

for(int col=0; col<8; col++){
  sum=sum+sales[row][col];
  System.out.println("The total is:" + sum);

public static String determineLocation(int row){
String name= " ";

if (row == 0){
 name= "Houston";

 else if (row==1){
  name="Dallas";

  else if (row== 2){
  name="Huntsville";

 else if (row== 3){
 name="San Antonio";

 else if( row == 4){
 name= "Waco";

 else if (row==5){
 name="Humble";

 return name;


  public static void unpopular(int[][] sales){
int count=0;
for (int row=0; row<6;row++){

for(int col=0; col<8; col++){
    if(sales[row][col]<3){
                count++;
    }
    System.out.println("unpopular model are:"+ count);
}


     public static void mostSales(int[][]sales){

   int min;
    for(int i=1 ;i<8; i++){
         if( sales.length <min){

              }
           System.out.println(min + "Sold the least anvils");


  public static void leastSales(int[][] sales){

int max;

for(int i=1 ;i<6; i++){
   if(sales.length>max){

   System.out.println(max + "Sold the most anvils");

【问题讨论】:

  • 您愿意与我们分享您的一些代码吗?而不仅仅是分配?
  • 这是一个问答网站,而不是程序员招聘网站。我认为您正在寻找后者。
  • 对不起,我刚刚学会了如何添加我的程序

标签: java arrays arraylist


【解决方案1】:
    int[][] sales = new int[6][8];

    // a
    for (int i = 0; i < sales.length; i++) {
        int sum = 0;
        for (int j = 0; j < sales[i].length; j++) {
            sum += j;
        }
        System.out.println("Location " + String.valueOf(i) + " has "
                + String.valueOf(sum) + " anvils sold");
    }

    // b
    for (int i = 0; i < sales[0].length; i++) {
        int unpopular = 0;
        for (int j = 0; j < sales.length; j++) {
            if (sales[j][i] > 0) {
                unpopular++;
            }
            if (unpopular >= 3) {
                break;
            }
        }
        if (unpopular < 3) {
            System.out.println("Modle " + String.valueOf(i)
                    + " is un popular");
        }
    }

    // c3
    int max = -1;
    int maxLocation = -1;
    for (int i = 0; i < sales.length; i++) {
        int sum = 0;
        for (int j = 0; j < sales[i].length; j++) {
            sum += j;
        }
        if (sum > max) {
            max = sum;
            maxLocation = i;
        }
    }
    System.out.println("Location " + String.valueOf(maxLocation)
            + " has most hardworking employees");

    // d
    int min = 25 * 8 + 1;
    ;
    int minLocation = -1;
    for (int i = 0; i < sales.length; i++) {
        int sum = 0;
        for (int j = 0; j < sales[i].length; j++) {
            sum += j;
        }
        if (sum < min) {
            min = sum;
            minLocation = i;
        }
    }
    System.out.println("Location " + String.valueOf(minLocation)
            + " has most hardworking employees");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2013-04-30
    • 2016-08-22
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多