带有getter的if语句问题

时间:2016-02-17 01:24:08

标签: java eclipse if-statement getter-setter

我的任务是开发一个测试我的Robot类的应用程序。测试应用程序将询问用户机器人名称。然后它将使用名称实例化Robot对象。然后,应用程序将使用循环来要求用户输入要移动的方向(x或y)和距离。当用户输入q的方向(退出)时,循环将终止。循环完成后,程序将显示机器人的最终位置和行进的总距离。

我的主要问题是在我的if语句中尝试使用我的Robot类并在我的Prog3类中测试它。这是我的机器人课程:

public class Robot {
private String name;
private int xPosition;
private int yPosition;
private int totalDistance;

public Robot(String name, int xPosition, int yPosition, int totalDistance){
    super();
    this.name = name;
    this.xPosition = 0;
    this.yPosition = 0;
    this.totalDistance = totalDistance;
}

public String getName() {
    return name;
}

public int getxPosition() {
    return xPosition;
}

public int getyPosition() {
    return yPosition;
}

public int getTotalDistance() {
    return totalDistance;
}

public void moveX(int distance){
    xPosition += distance;
    totalDistance += Math.abs(distance);
}

public void moveY(int distance){
    yPosition += distance;
    totalDistance += Math.abs(distance);
}



}

现在这是我的Prog3课程:

import java.util.Scanner;
public class Prog3 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Robot robot = new Robot();

    //asks the user for robots name
    System.out.print("Enter robots name; ");
    Robot.name = in.nextLine();

    //asks the user to move the robot
    System.out.print("Direction of move (x/y/q): ");
    String move = in.nextLine();

    if( move.equals("x") ){
        System.out.print("Distance: ");

    }
    else if( move.equals("y") ){
        System.out.print("Distance: ");

    }
    else{


    }

}
}

正如你所看到的,我的Prog3课程并不完整,因为我完全陷入困境。非常感谢您的时间,非常感谢。

2 个答案:

答案 0 :(得分:1)

“当用户输入q的方向(退出)时,循环将终止。”你的程序没有循环:

 Scanner in = new Scanner(System.in);
Robot robot = new Robot();

//asks the user for robots name
System.out.print("Enter robots name; ");
Robot.name = in.nextLine();

//asks the user to move the robot
System.out.print("Direction of move (x/y/q): ");
String move = in.nextLine();

if( move.equals("x") ){
    System.out.print("Distance: ");

}
else if( move.equals("y") ){
    System.out.print("Distance: ");

}
else{


}

此代码段只会运行一次......如果你想循环它,你需要一个实际的

 While(<Conditional>) { 
// your code here
// a break point to exit the loop ( in your case "q" as the input string )
} // end while...

我也有点困惑,因为你的问题的措辞有点含糊不清。您问题的本质究竟是什么?

答案 1 :(得分:0)

你的构造函数没有意义,为什么你需要输入x和y值,如果它们总是设置为0?

我写了一个新的空白构造函数,并添加了一些setter方法

public class Robot {
private String name;
private int xPosition;
private int yPosition;
private int totalDistance;

public Robot(){
    name = null;
    xPosition = 0;
    yPosition = 0;
    totalDistance = 0;
}

public Robot(String name, int xPosition, int yPosition, int totalDistance){
    super();
    this.name = name;
    this.xPosition = 0;
    this.yPosition = 0;
    this.totalDistance = totalDistance;
}

public String getName() {
    return name;
}

public void setName( String name){
    this.name = name;
}

public int getxPosition() {
    return xPosition;
}

public void setxPosition(int x) {
    xPosition = x;
}

public int getyPosition() {
    return yPosition;
}

public void setyPosition(int y){
    yPosition = y;
}

public int getTotalDistance() {
    return totalDistance;
}


public void moveX(int distance){
    xPosition += distance;
    totalDistance += Math.abs(distance);
}

public void moveY(int distance){
    yPosition += distance;
    totalDistance += Math.abs(distance);
}

然后你需要对你的主要课程中的所有内容做些什么

import java.util.Scanner;
public class Prog3 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Robot robo = new Robot();

    //asks the user for robots name
    System.out.print("Enter robots name: ");
    String myName = in.next();
    robo.setName(myName);


    while( true ){
    //asks the user to move the robot
    System.out.print("Direction of move (x/y/q): ");
    String direction = in.next();
    if(direction.equals("q"))
        break;

    System.out.println("Distance of move: ");
    int distance = in.nextInt();


    if (direction.equals("x")) {
        robo.moveX(distance);
        System.out.println("moved" + distance +"units along the x axis");
    }
    else if (direction.equals("y")) {
        robo.moveY(distance);
        System.out.println("moved " + distance +" units along the y axis");
    }
    else {
        System.out.println("error");
    }
    }

    System.out.println("moved " + robo.getTotalDistance() + " total distance");

}
}

如果您还有其他问题,请与我们联系。