Java学习第39天2020/8/13
一.
import java.util.Scanner; public class point { Scanner input=new Scanner(System.in); private double x,y,z; public void in() { x=input.nextDouble();y=input.nextDouble();z=input.nextDouble(); } public void out(){ System.out.println("("+x+","+y+","+z+")"); } public void get() { System.out.println("x="+x+" "+"y="+y+" "+"z="+z+" "); } public void distance() { double p=x*x+y*y+z*z; System.out.println("到原点的距离是:"+Math.sqrt(p)); } public static void main(String []args) { point m=new point(); m.in(); m.out(); m.get(); m.distance(); } }
math.sqrt(double n);//其中n是被开方的数
如要给m开n次方就用:
java.lang.StrictMath.pow(m,1.0/n);
import java.util.Scanner; public class point extends line{ Scanner input=new Scanner(System.in); private double x,y,z; public void in() { x=input.nextDouble();y=input.nextDouble();z=input.nextDouble(); } public void out(){ System.out.println("("+x+","+y+","+z+")"); } public double get_x() { return x; } public double get_y() { return y; } public double get_z() { return z; } public void distance() { double p=x*x+y*y+z*z; System.out.println("到原点的距离是:"+Math.sqrt(p)); } }
public class line {
public static void main(String []args) {
point []a=new point[2];
System.out.println("输入两点的坐标:");
for(int i=0;i<2;i++)
{
a[i]=new point();
}
for(int i=0;i<2;i++) {
a[i].in();}
double l=Math.pow(a[0].get_x()-a[1].get_x(), 2)+Math.pow(a[0].get_y()-a[1].get_y(), 2)+Math.pow(a[0].get_z()-a[1].get_z(), 2);
System.out.println("两点之间的距离为:"+Math.sqrt(l));
}
}
二.math.pow(x,2)
math.sqrt(l);
三.例题