java 语言问题 编程语法

凌峰520 发布于 2016/12/10 09:12
阅读 158
收藏 1


import java.util.Scanner;
import java.lang.Math;
class Point2D 
{
int x;
int y;
public Point2D(int i,int j)
{
x=i;
y=j;
}
public void offset(int a,int b)
{
x=x+a;
y=y+b;
}
}
 class Point3D extends Point2D
{
int x;
int y;
int Z;
public Point3D(int x, int y,int z) 
{
super(x,y);
Z=z;
}
public Point3D(Point2D p,int z)
    {
    super(p.x,p.y);
    Z=z;
    }
public void offset(int a,int b,int c)
{
x+=a;
y+=b;
Z+=c;
}
public static void main(String args[])
{
int x=0,y=0,x1=0,y1=0,a=0,b=0,c=0,a1 = 0,b1=0,c1=0;
double sum1=0;
double sum2=0;
Scanner reader=new Scanner(System.in);
System.out.println("输入整数坐标x和y");
x=reader.nextInt();
y=reader.nextInt();
Point2D p2d1=new Point2D(x,y);
//Point2D p2d1=new Point2D(1,1);
System.out.println("输入整数坐标x1和y1");
x1=reader.nextInt();
y1=reader.nextInt();
Point2D p2d2=new Point2D(x1,y1);
//Point2D p2d2=new Point2D(2,2);
sum1=Math.sqrt(Math.pow((p2d1.x-p2d2.x),2)+Math.pow((p2d1.y-p2d2.y),2));
System.out.println("("+x+","+y+")"+"与"+"("+x1+","+y1+")"+"的距离为:"+sum1);
System.out.println("输入整数坐标a,b和c");
a=reader.nextInt();
b=reader.nextInt();
c=reader.nextInt();
Point3D p3d1=new Point3D(a,b,c);
System.out.println("输入整数坐标a1,b1和c1");
a1=reader.nextInt();
b1=reader.nextInt();
c1=reader.nextInt();
Point3D p3d2=new Point3D(a1,b1,c1);
sum2=Math.sqrt(Math.pow((p3d1.x-p3d2.x),2)+Math.pow((p3d1.y-p3d2.y),2)+Math.pow((p3d1.Z-p3d2.Z),2));
System.out.println("("+a+","+b+","+c+")"+"与"+"("+a1+","+b1+","+c1+")"+"的距离为:"+sum2);
}
}
代码如上  计算sum2是  总是只输出最后一项表达式的值  没有计算前两个坐标点的差  请教大神帮忙跳出问题所在  
加载中
返回顶部
顶部