【问题标题】:Java- Involving objects and multiple classesJava-涉及对象和多个类
【发布时间】:2014-10-18 14:53:31
【问题描述】:

我的问题是对于 getTime();命令,您需要所有速度、处理、xcord、ycord 和 terrainDifficultry 变量才能得到答案,但我只能调用 getTime();来自 mb1 类。基本上,当我到达 System.out getTime() 时,我一直得到 0.0,但我不知道如何修复它。

import java.util.Scanner;
public class Main_MoonRace {

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.println("Enter the speed of the moonbuggy as an integer.");
        int s = keyboard.nextInt();
        System.out.println("Enter the handling of the moonbuggy (between 0-0.9)");
        double h = keyboard.nextDouble();
        moonbuggy mb1 = new moonbuggy(s,h);

        System.out.println("Enter the x-coordinate of where the moonbuggy will be headed to as an integer.");
        int xcord = keyboard.nextInt();
        System.out.println("Enter the y-coordinate of where the moonbuggy will be headed to as an integer.");
        int ycord = keyboard.nextInt();
        System.out.println("Enter the difficulty of the terrain that the moonbuggy will be experiencing (integer from 1-10).");
        int terrainDifficulty = keyboard.nextInt();
        MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty);

        System.out.println(mb1.getTime());
    }
}

moonbuggy.java

public class moonbuggy {    
    private int speed = 1;
    private double handling = 0;
    moonbuggy(){
        return;
    }
    moonbuggy(int s, double h){
        speed = s;
        handling = h;

        return;
    }
    public void setSpeed (int s){
        speed = s;
    }
    public void setHandling (double h){
        handling = h;
    }
    public int getSpeed(){
        return speed;
    }
    public double getHandling(){
        return handling;
    }
    MoonLocation obj1 = new MoonLocation();

    public double getTime(){
        double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling())));
        return time;
    }
}

MoonLocation.java

public class MoonLocation {
    private int x = 0;
    private int y = 0;
    private int terrain = 1;

    MoonLocation(){
        return;
    }
    MoonLocation(int xcord, int ycord, int terrainDifficulty){
        x= xcord;
        y = ycord;
        terrain = terrainDifficulty;

        return;
    }

    public void setX (int xcord){
        x = xcord;
    }
    public void setY (int ycord){
        y = ycord;
    }
    public void setTerrain (int terrainDifficulty){
        terrain = terrainDifficulty;
    }
    public int getX () {
        return x;
    }
    public int getY () {
        return y;
    }
    public int getTerrain () {
        return terrain;
    }
    public double getdistance () {
        double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2)));
        return distance;
    }
}

【问题讨论】:

  • 您正在构建一个MoonLocation 然后完全忽略它的事实表明存在问题......但您还没有真正提出问题,这很难帮助您。 ..
  • 你需要给moonbuggy的代码

标签: java class object methods


【解决方案1】:

查看moonbuggy 类中的这部分代码(注意,按照惯例,java 中的类应始终以大写字母开头)。

MoonLocation obj1 = new MoonLocation();
public double getTime(){
    double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling())));
    return time;
}

您实例化一个不带任何参数的MoonLocation,然后在getTime 方法中访问它。这解释了为什么在调用 getTime 时总是得到 0.0。

现在将您的getTime 方法修改为

public double getTime(MoonLocation location){
    return (((location.getdistance())/(getSpeed()))*(location.getTerrain())*(1-(getHandling())));
}

请注意,我删除了时间变量,因为它在那里完全没用。

并改变你的主要

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty);
System.out.println(mb1.getTime());

MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty);
System.out.println(mb1.getTime(mL1));

另外,请删除 moonbuggy 类中未使用的 MoonLocation obj1 = new MoonLocation()

【讨论】:

    【解决方案2】:

    问题在于您的代码。首先,您在 main() 方法下的 Main_MoonRace 类中创建 MoonLocation 对象:

    MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty);
    

    这里创建了一个 MoonLocation 对象,并使用 xcord、ycord 和 terrainDifficulty 值进行初始化。

    现在,在您的 MoonBuggy 类中,您再次创建 MoonLocation 的对象:

    MoonLocation obj1 = new MoonLocation();
    

    这里只创建了一个 MoonLocation 类的空对象。

    现在,当你打电话时:

    obj1.getDistance();   It will return 0 only.
    

    以下是 MoonBuggy 类的更正代码。

    public class Moonbuggy {    
        private int speed = 1;
        private double handling = 0;
    
        Moonbuggy(){}
    
        Moonbuggy(int s, double h){
            speed = s;
            handling = h;
        }
        public void setSpeed (int s){
            speed = s;
        }
        public void setHandling (double h){
            handling = h;
        }
        public int getSpeed(){
            return speed;
        }
        public double getHandling(){
            return handling;
        }
    
        private MoonLocation obj1;
    
        public MoonLocation getObj1() {
            return obj1;
        }
    
        public void setObj1(MoonLocation obj1) {
            this.obj1 = obj1;
        }
    
        public double getTime(){
            double time = (((obj1.getdistance())/(getSpeed()))*(obj1.getTerrain())*(1-(getHandling())));
            return time;
        }
    }
    

    在 main() 方法中添加一个:

    MoonLocation mL1 = new MoonLocation(xcord,ycord,terrainDifficulty);
            mb1.setObj1(mL1);  // set the MoonLocation object
            System.out.println(mb1.getTime());
    

    现在,你会得到正确的输出

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 2019-11-27
      • 2015-05-27
      • 2021-08-05
      相关资源
      最近更新 更多