带有 getter 的 if 语句的问题
Issue with if-statement with getters
我的任务是开发一个应用程序来测试我的机器人 class。测试应用程序将询问用户机器人名称。然后它将使用该名称实例化一个 Robot 对象。然后应用程序将使用循环要求用户输入移动方向(x 或 y)和距离。当用户输入方向 q(退出)时,循环将终止。循环完成后,程序将显示机器人的最终位置和总行进距离。
我的主要问题是尝试使用我的机器人 class 并在涉及我的 if 语句时在我的 Prog3 class 中测试它。这是我的机器人 class:
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 class:
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 class 还没有完成,因为我完全卡住了。非常感谢您的宝贵时间。
"The loop will terminate when the user enters a direction of q (for quit)."。您的程序没有循环:
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...
另外我有点困惑,因为你问题的措辞有点含糊。您的问题的性质究竟是什么?
您的构造函数没有意义,如果 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);
}
然后你需要在你的 main class
中对所有这些做一些事情
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");
}
}
如果您还有其他问题,请告诉我。
我的任务是开发一个应用程序来测试我的机器人 class。测试应用程序将询问用户机器人名称。然后它将使用该名称实例化一个 Robot 对象。然后应用程序将使用循环要求用户输入移动方向(x 或 y)和距离。当用户输入方向 q(退出)时,循环将终止。循环完成后,程序将显示机器人的最终位置和总行进距离。
我的主要问题是尝试使用我的机器人 class 并在涉及我的 if 语句时在我的 Prog3 class 中测试它。这是我的机器人 class:
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 class:
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 class 还没有完成,因为我完全卡住了。非常感谢您的宝贵时间。
"The loop will terminate when the user enters a direction of q (for quit)."。您的程序没有循环:
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...
另外我有点困惑,因为你问题的措辞有点含糊。您的问题的性质究竟是什么?
您的构造函数没有意义,如果 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);
}
然后你需要在你的 main class
中对所有这些做一些事情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");
}
}
如果您还有其他问题,请告诉我。