gpt4 book ai didi

java - 从位置类获取 "getDistance"并使用 if 条件在目标类中使用它

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:15 25 4
gpt4 key购买 nike

//单独文件中的位置类

package tld;
import java.util.*;
public class Location {
private double x;
private double y;
Scanner scan= new Scanner(System.in);

public Location(double x, double y){
x= scan.nextDouble();
y= scan.nextDouble();}

public double getX(){
return x;}

public double getY(){
return y;}

public void setX(double x){
this.x=x;}

public void setY(double y){
this.y=y;}

public String toString(){
return "Location: x= "+getX()+" ; y= "+getY();}

public double getDistance(Location L){
double x1=scan.nextDouble();
double y1=scan.nextDouble();
L=new Location(x1,y1);
return (Math.sqrt((Math.pow((x-x1),2))+(Math.pow((y-y1), 2))));}
}

.

//目标类在单独的文件中

package tld;
import java.util.*;
public class Target{
Scanner scan = new Scanner(System.in);
private Location loc;
private double speed;
private String type;
private boolean threat;
private int count = 0;

public Target(double x, double y, double speed){
x=scan.nextDouble();
y=scan.nextDouble();
speed=scan.nextDouble();
if(speed<500)
type="Helicopter";
else type="Jet";
threat = false;
count++;}

public String getType(){
return type;}

public double getSpeed() {
return speed;}

public void setSpeed(double speed) {
this.speed = speed;}

public Location getLoc() {
return loc;}

public void setLoc(Location loc) {
this.loc = loc;}

public boolean isThreat(Location L){
if(Location.getDistance()<100){
threat=true;}
else threat=false;
return threat;}

public String toString(){
return "Target is " + getType() + ", speed= " + getSpeed() + "mph, [Location: x=" + Location.x1+ "; y=" + Loc.x + "]";
}}

我的错误在这个类中..在“boolean isThreat 方法”中

.

//主驱动程序在一个单独的文件中,但尚未完成

package tld;
import java.util.*;
public class TLD {
public static void main(String[] args) {
Location L = new Location(100,200);
System.out.println("please enter 4 targets");
Scanner scan = new Scanner(System.in);
String [] Target=new String[4];
for(int i=0; i<Target.length; i++)
Target[i]=scan.next();


}

}

请注意,我不允许使用高级命令或类

最佳答案

由于 getDistance 不是静态的,因此您需要先拥有 Location 的实例,然后才能调用它。幸运的是,您确实有一个实例。看到类顶部声明的 loc 变量了吗?这就是您所需要的!

所以改变这一行

if(Location.getDistance()<100){

对此:

if(loc.getDistance(L)<100){

“为什么我必须把L放在那里?”你可能会问。那是因为我们正在获取两个位置之间的距离!第一个位置是 loc 但第二个位置是什么?从方法签名来看,我认为您希望参数 L 成为第二个位置。

此外,您的代码中还有很多没有意义的地方。这是我对您想要做什么的猜测:

public class Location {
private double x;
private double y;

public Location(double x, double y){
this.x = x;
this.y = y;
}

public double getX(){
return x;
}

public double getY(){
return y;
}

public void setX(double x){
this.x=x;
}

public void setY(double y){
this.y=y;
}

public String toString(){
return "Location: x= "+getX()+" ; y= "+getY();
}

public double getDistance(Location L){
double x1=L.getX();
double y1=L.getY();
return (Math.sqrt((Math.pow((x-x1),2))+(Math.pow((y-y1), 2))));
}
}

public class Target{
private Location loc;
private double speed;
private String type;
private boolean threat;
private static int count = 0;

public Target(double x, double y, double speed){
loc = new Location(x, y);
this.speed = speed;
if(speed<500)
type="Helicopter";
else type="Jet";
threat = false;
count++;
}

public String getType(){
return type;
}

public double getSpeed() {
return speed;
}

public void setSpeed(double speed) {
this.speed = speed;
}

public Location getLoc() {
return loc;
}

public void setLoc(Location loc) {
this.loc = loc;
}

public boolean isThreat(Location L){
if(loc.getDistance(L)<100){
threat=true;}
else threat=false;
return threat;
}

public String toString(){
return "Target is " + getType() + ", speed= " + getSpeed() + "mph, [Location: x=" + loc.getX()+ "; y=" + loc.getY() + "]";
}
}

关于java - 从位置类获取 "getDistance"并使用 if 条件在目标类中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com