幫助請.... 我是Java新手。我正在嘗試在Eclipse(Luna)的公共類「PeterAndSnowBlower」中編寫一個包含類「點」的代碼。 我也嘗試通過在類「point」中公開變量x,y並給出相同的錯誤。 我也用this.x和this.y代替x和y的構造函數裏面構造函數沒有在eclipse中的java中定義,雖然它被定義爲
這裏是我的代碼:
import java.util.*;
public class PeterAndSnowBlower {
class point{
int x;
int y;
public point() {
x = y = 0;
}
public point(int a, int b){
x = a;
y = b;
}
public point(point p) {
x = p.x;
y = p.y;
}
public double distance(point P){
int dx = x - P.x;
int dy = y - P.y;
return Math.sqrt(dx*dx + dy*dy);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n, x, y;
point P = new point(0, 0);
n = in.nextInt();
P.x = in.nextInt();
P.y = in.nextInt();
Vector<point>points = new Vector<point>();
for(int i = 0; i < n; i++){
x = in.nextInt();
y = in.nextInt();
points.add(new point(x, y));
}
double r1, r2;
r1 = r2 = P.distance(points.get(0));
for(point point:points){
r1 = Math.max(r1, P.distance(point));
}
}
}
的錯誤是:
Multiple markers at this line
- No enclosing instance of type PeterAndSnowBlower is accessible. Must qualify the allocation with an enclosing instance of type PeterAndSnowBlower (e.g.
x.new A() where x is an instance of PeterAndSnowBlower).
- The constructor PeterAndSnowBlower.point(int, int) is undefined
不是按類別創建類。特別是如果你是新手,開始沒有IDE來學習基礎知識。 – Stultuske
如果你想要一個靜態訪問,使你的''''類'靜態' –