gpt4 book ai didi

java - 在点数组中输入一个值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:07 24 4
gpt4 key购买 nike

所以我想把点的两个坐标输入到数组中。抱歉,如果这离题太远了,我很困惑。

//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();
}
}

最佳答案

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;
}
...

关于java - 在点数组中输入一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com