Java中此代码的输出是什么?

时间:2016-11-28 06:41:11

标签: java class

我试图运行代码并收到错误"点无法解析为变量"。为什么我收到此错误,请尝试自己运行代码并分享您获得的输出。感谢。

public class ReferenceMystery1 {
    public static void main(String[] args) {
        int a = 7;
        int b = 9;
        Point p1 = new Point(2,2);
        Point p2 = new Point(2,2);
        addToXTwice(a, p1);
        System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
        addToXTwice(b, p2);
        System.out.println(a + " " + b + " " + p1.x + " " + p2.x);

    }

    public static void addToXTwice(int a, Point b) {
        a = a + a;
        p1.x = a;
        System.out.println(a + " " + p1.x);
    }
}

2 个答案:

答案 0 :(得分:1)

导入点类

import java.awt.Point;

替换

p1.x = a;
System.out.println(a + " " + p1.x);

b.x = a;
System.out.println(a + " " + b.x);

完整的工作代码是

import java.awt.Point;
public class HelloWorld{

public static void main(String[] args) {
    int a = 7;
    int b = 9;
    Point p1 = new Point(2,2);
    Point p2 = new Point(2,2);
    addToXTwice(a, p1);
    System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
    addToXTwice(b, p2);
    System.out.println(a + " " + b + " " + p1.x + " " + p2.x);
}

public static void addToXTwice(int a, Point b) {
    a = a + a;
    b.x = a;
    System.out.println(a + " " + b.x);
    }
}

答案 1 :(得分:0)

"点"我没办法上课程。

假设它可用并且您能够编译,那么,如前面的响应所示......您无法在addToXTwice()方法中访问p1.x,因为输入参数是' a&#39 ;和' b'。

使用" import java.awt.Point",我收到以下编译错误

  

javac -g ReferenceMystery1.java   ReferenceMystery1.java:18:错误:找不到符号       p1.x = a;       ^     符号:变量p1     location:class ReferenceMystery1   ReferenceMystery1.java:19:错误:找不到符号       System.out.println(a +"" + p1.x);                                    ^     符号:变量p1     location:class ReferenceMystery1   2个错误

这些与以前的评论一致:" p1"不能从addToXTwice()方法中获得。