初始化二维数组并显示它们

时间:2015-04-16 01:35:24

标签: java

我试图通过循环显示和初始化我的二维数组。然而,eclipse给了我一个错误:

"线程中的异常" main" java.lang.Error:未解决的编译问题: at lastassignment2.ModifyCar.main(ModifyCar.java:75)"

这是我的代码:    package lastassignment2;

class Car {

private String brand;
private long serialNumber;
private double Price;

public Car (String br, long sN, double Pr)
{
    brand= br;
    serialNumber = sN;
    Price = Pr;
}
 public Car (Car aCar)
  {
    this(aCar.getbrand(), aCar.getserialNumber(), aCar.getPrice());
   }
 public String getbrand()
 {
    return brand;
 }
 public long getserialNumber()
 {
     return serialNumber;
 }
 public double getPrice()
 {
   return Price;
 }
 public void setBrand(String carBrand)
  {

     //allows to set the brand of the carphone
    brand = carBrand;
}
public void setSerialNumber(long SN)
{
     // Sets the Serial Number of the car

    serialNumber = SN;
}
public void setPrice(double Pr)
{
     // Sets the price of the car

    Price = Pr;
}
public String toString()
{
    return this.brand + ", " + this.serialNumber + " " + this.Price;

}
public boolean equals(Car phone)
{
    if (Price == phone.Price  && brand.equals(phone))
        return true;
    else
        return false;}
public boolean equals2(Car phone)
{ if (Price == phone.Price)
    return true;
else
    return false; 
}
public boolean equals3(Car phone)
{ if (brand.equals(phone));
    return true;
}

}
 public class ModifyCar {
 public static void main (String[] args)
 {    
     Car[][] carArr  = new Car[10] [10];
     for (int i=0; i< CarArr.length-1; i++)
       { for (int m=0; m<carArr[m].length; m++)
          {  if (i % 3 == 0)
            carArr[i][m]= new Car("Samsung", 00000001 + (2 * i+1), 500.4 + i);
          else if (i % 3 == 1)
              carArr[i][m] = new Car("LG", 0000001 * (2 * i), 500.6 + i);
          }
     for (int y=9; y<carArr.length; y++)
        { for (int n=0; n<carrArr[y].length; n++)
             { 
               carArr[y][n] = new Car(carArr [y-7][n]); 
              }
         }

     for (int p = 0; p < carArr.length; p++) {

            // Loop and display sub-arrays.
            for (int x = 0; x < carArr[p].length; x++) {
            System.out.print(carfArr[p][x] + " ");
            }
            System.out.println();
       }


     }
    }
   }

我试图通过for循环初始化所有内容(主要用于练习,因为我对他们有点弱)

创建了最后2个for循环,以便通过表格格式表示数组。

2 个答案:

答案 0 :(得分:0)

您的主要方法有一些编译问题:

以下是更正后的代码:

 public static void main (String[] args)
 {    
     Car[][] carArr  = new Car[10] [10];
     for (int i=0; i< carArr.length-1; i++)
       { for (int m=0; m<carArr[m].length; m++)
          {  if (i % 3 == 0)
            carArr[i][m]= new Car("Samsung", 00000001 + (2 * i+1), 500.4 + i);
          else if (i % 3 == 1)
              carArr[i][m] = new Car("LG", 0000001 * (2 * i), 500.6 + i);
          }
     for (int y=9; y<carArr.length; y++)
        { for (int n=0; n<carArr[y].length; n++)
             { 
               carArr[y][n] = new Car(carArr [y-7][n]); 
              }
         }

     for (int p = 0; p < carArr.length; p++) {

            // Loop and display sub-arrays.
            for (int x = 0; x < carArr[p].length; x++) {
            System.out.print(carArr[p][x] + " ");
            }
            System.out.println();
       }


     }
    }

答案 1 :(得分:0)

我建议你使用eclipse IDE它会帮助你解决像这样的编译问题。你有3个拼写错误,内部课程不能公开:

  1. 从第70行删除公众
  2. CarArr to carArr line 74
  3. carrArr to carArr line 82
  4. carfArr to carArr line 92