Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在寻找原点和远端点之间的距离时,如何解决该算法?_Java_Oop - Fatal编程技术网

Java 在寻找原点和远端点之间的距离时,如何解决该算法?

Java 在寻找原点和远端点之间的距离时,如何解决该算法?,java,oop,Java,Oop,主类 import java.util.*; 公共课Q6_7_Muhammed_Irshath{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 TwoDPoint TwoDPoint=新建TwoDPoint();//为TwoDPoint类创建了一个对象 Scanner sc=new Scanner(System.in);//创建它是为了测试输入 对于(int i=0;i

主类

import java.util.*;
公共课Q6_7_Muhammed_Irshath{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
TwoDPoint TwoDPoint=新建TwoDPoint();//为TwoDPoint类创建了一个对象
Scanner sc=new Scanner(System.in);//创建它是为了测试输入
对于(int i=0;i<1;i++){
System.out.println(“请输入x:”)的值;
double x=sc.nextDouble();
System.out.println(“请输入y:”)的值;
双y=sc.nextDouble();
}            
}
}
双点类

import java.util.*;
公共类双点{
私有静态int x;
私人静态智力;
双点(){
x=0;
y=0;
}
双点(双x,双y){
}
公共静态int getX(){
返回x;
}
公共静态int getY(){
返回y;
}
公共双FindInstance(双点远程点){
双倍距离=0;
getX();
TwoDPoint.getY();
距离=
返回0;
}
}
我有一个算法,我必须找到
原点(0,0)
远程点(x,y)
之间的距离,必须读取输入。我的问题是如何使用
twodpoint对象
检索(x,y)并将其放入
twodpoint类
中的
findDistance
方法中?那么如何计算距离呢


请不要评判我,我是新手。我甚至不能解决一个简单的问题。这是我必须使用的解决算法的方法

可以尝试这种方法,并根据需要调整输入(从控制台等)

输出:
5.0
用于
x=3
y=4
或反向。。。

该公式基于
皮塔戈拉定理
,以
(0,0)
作为原点

可以尝试此方法并根据需要调整输入(从控制台等)

输出:
5.0
用于
x=3
y=4
或反向。。。

该公式基于
Pitagora定理
,以
(0,0)
作为原点给出

Google:@johnymapp我看到了,但我只想改进代码Google:@johnymapp我看到了,但我只想改进代码现在检查一下,但如何连接到各种输入基本上应该是一个简单的任务…现在检查一下,但如何连接到各种输入基本上应该是一个简单的任务。。。
import java.util.Scanner;

public class SolveTwoP {
double x,y;
public static void main(String[] args)
{
    SolveTwoP sol = new SolveTwoP();

    Scanner sc = new Scanner(System.in); // Created this to test input

    //no need loop here
    System.out.println("Please enter value for x: ");
    sol.x = sc.nextDouble();
    System.out.println("Please enter value for y: ");
    sol.y = sc.nextDouble();                 

    double result = sol. new TwoDPoint().findDistance(sol.new TwoDPoint(sol.x,sol.y));
    System.out.println(result);

    //get an instance of solver and invoke with the point on x=3, y=4
    //solver TwoDPoint is also assign the coordinates

    //double result = sol. new TwoDPoint().findDistance(sol.new TwoDPoint(3,4));
    //System.out.println(result);

}
//inner class
class TwoDPoint {

    private  double x;
    private  double y;

    TwoDPoint()
    {
        x = 0;
        y = 0;  
    }
    TwoDPoint(double x, double y){
        this.x=x;
        this.y=y;
    }

    public double getX() {
        return x;
    }
    public Double getY() {
        return y;
    }

    public double findDistance(TwoDPoint remotepoint)
    {
        double distance = remotepoint.getX()*remotepoint.getX() + remotepoint.getY()*remotepoint.getY();
        distance = Math.sqrt(distance);
        return distance;   
    }
}
}