照着《java语言程序设计》(Danial.Liang)的程序打的
为什么提示
已在方法 main(String[])中定义了变量 p1
代码如下
import java.util.Scanner;
import javafx.geometry.Point2D;
public class Test
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("enter point1's x-,y-coordinates:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.println("enter point2's x-,y-coordinates:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point2D p1 = new Point2D(x1, y1);
Point2D p1 = new Point2D(x2, y2);
System.out.println("p1 is"+p1.toString());
System.out.println("p2 is"+p2.toString());
System.out.println("the distance between p1 and p2 is"+p1.distance(p2));
}
}
Point2D p1 = new Point2D(x1, y1);
Point2D p1 = new Point2D(x2, y2);
p1重复了
在同一个方法中定义了两个变量p1。修改建议:要么改第二个为p2,要么将第二个p1前面的类型去掉。
变量可以重复,就看作用域了,方法的作用域,在方法的花括号里,是不能重复的
写代码注意一点,这样的代码细心就好了,大牛这是这样炼成的
Point2D p1 = new Point2D(x1, y1);
Point2D p1 = new Point2D(x2, y2);,同志,你定义了两个p1