-1

我正在尝试将四个单独DigitalCamera的对象更改为四个对象的数组DigitalCamera,这是代码,我无法得到它,有什么建议吗?

import java.util.Scanner;
public class TestDigitalCamera
{

public static void main(String[] args)
{
  DigitalCamera camera1;
  DigitalCamera camera2;
  DigitalCamera camera3;
  DigitalCamera camera4;

  camera1 = getData();
  camera2 = getData();
  camera3 = getData();
  camera4 = getData();

  camera1.display();
  camera2.display();
  camera3.display();
  camera4.display(); 
}
  public static DigitalCamera getData()
  {
  String brand;
  int megapixels;
  Scanner in = new Scanner(System.in);
  System.out.print("Enter camera brand ");
  brand = in.nextLine();
  System.out.print("Enter megapixels ");
  megapixels = in.nextInt();
  in.nextLine();
  DigitalCamera c = new DigitalCamera(brand, megapixels);
  return c;

  }
4

1 回答 1

4

只需创建一个数组并使用循环。尝试这个:

DigitalCamera[] cameraArray = new DigitalCamera[4];
for(DigitalCamera camera:cameraArray) {
   camera = getData();
   camera.display();
}
于 2013-11-08T02:52:05.043 回答