2015-10-27 94 views
3

所以我想輸入點的兩個座標到數組中。對不起,如果這是離開,我很困惑。將值輸入到點數組中?

//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

A[i].input(in);無效語法。您必須將新的Point對象添加到陣列中。爲了做到這一點,您必須從用戶那裏獲得足夠的信息來創建一個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(int x, int y) { 
    this.x = x; 
    this.y = y; 
} 
... 
+0

感謝您的回覆,但我仍然迷失。我收到了錯誤「線程中的異常」main「java.lang.UnsupportedOperationException」,我更新了帖子以添加我的Point.java。 – Joe123

+0

沒關係,我想我明白了。謝謝 – Joe123