在点数组中输入值?

时间:2015-10-27 00:27:02

标签: java

所以我想将点的两个坐标输入到数组中。对不起,如果这样的话,我很困惑。

//create array of 100 coordinate points
//Excerpt from Main

Point[] A = new Point[100];


Scanner in = new Scanner(System.in);

System.out.println("Enter index: ");

int i = in.nextInt(); //validate

System.out.print("Enter integers x, y to replace: ");

A[i].input(in);

...

public class Point {

  int x, y;

  Point(int x, int y) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  void input(Scanner sc){
    x = in.nextInt();
    y = in.nextInt();
  }
}

1 个答案:

答案 0 :(得分:1)

A[i].input(in);是无效的语法。您必须将新的Point对象添加到数组中。为此,您必须从用户那里获得足够的信息才能创建Point对象。

你想做更接近这件事的事情:

//create array of 100 coordinate points

Point[] A = new Point[100];


Scanner in = new Scanner(System.in);

System.out.println("Enter index: ");

int i = in.nextInt(); //validate

System.out.print("Enter integers x, y to replace: ");

int x = in.nextInt();
int y = in.nextInt();

a[i] = new Point(x, y);

Point的构造函数中,您抛出错误。删除引发错误的行,而不是使用它来分配值。

...
Point(int x, int y) {
  this.x = x;
  this.y = y;
}
...