我被要求: 创建EntertainmentRobot对象和HumanStudyRobot对象(使用先前的数据 给出)并将这些对象存储在数组列表中。 使用本文前面概述的数据设置阵列中每个机器人的用途。 使用toString方法打印每个机器人的详细信息。
我已经这样做了,我的代码如下。但是当我运行代码时,添加到我的arrayList的每个机器人的细节每次打印两次。知道如何修复它,以便数组列表中每个机器人对象的细节只打印一次吗?非常感谢
package Program;
import java.util.ArrayList;
import java.util.Scanner;
public class Tester04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Robot> Robots = new ArrayList<Robot>();
HumanStudyRobot HumanStudyRobot1 = new HumanStudyRobot("HRP", 1.5, 58.0, "Kawada Industries");
HumanStudyRobot1.setPurpose("Study into human movement and perform a wide range of tasks.");
EntertainmentRobot EntertainmentRobot1 = new EntertainmentRobot("QRIO", 0.6, 7.3, "SONY");
EntertainmentRobot1.setPurpose("To live with you, make life fun and make you happy.");
Robots.add(EntertainmentRobot1);
Robots.add(HumanStudyRobot1);
for (Robot eachRobot : Robots) { //for loop to go through the array list
System.out.println(Robots.toString());
System.out.println("\n");
}
startRobot(Robots, input);
}
public static void startRobot(ArrayList<Robot> Robots, Scanner input){
for( int i = 0 ; i < Robots.size(); i++ ) {
Robots.get(i).start();
Robots.get(i).doTask();
Robots.get(i).doTask(input);
Robots.get(i).stop();
}
}
}
package Program;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class EntertainmentRobot extends Robot {
//constructor
public EntertainmentRobot(String name, double height, double weight, String manufacturer) {
super(name, height, weight, manufacturer);
this.energy = 10.0;
this.EnergyUnitsRequired = 0.75;
}
@Override
public void start() {
System.out.println("This is an Entertainment Robot. \nThe robot has started entertaining.");
}
@Override
public void stop() {
System.out.println("I have stopped entertaining people.");
System.out.println("--------------------------------------------------");
}
@Override
public void doTask(Scanner input) {
play();
talk();
walk();
}
@Override
public void doTask() {
// TODO Auto-generated method stub
}
public void talk() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a phrase for me to repeat");
String phrase = input.nextLine();
System.out.println("You have asked me to say the following phrase : " + phrase);
}
public void walk() {
Scanner input = new Scanner (System.in);
System.out.println("Would you like me to walk for you?");
if (input.nextLine().equalsIgnoreCase("Y")) {
System.out.println("For how many paces? ");
int steps = input.nextInt();
input.nextLine();
System.out.println("Which direction do you want me to walk? (Enter a number between 1 and 4.) ");
System.out.println("1 - " + Directions.FORWARD);
System.out.println("2 - " + Directions.BACKWARD);
System.out.println("3 - " + Directions.LEFT);
System.out.println("4 - " + Directions.RIGHT);
int selection = input.nextInt();
if (selection == 1 ) {
setDirection(Directions.FORWARD);
} else
if (selection == 2 ) {
setDirection(Directions.BACKWARD);
} else
if (selection == 3 ) {
setDirection(Directions.LEFT);
} else
if
(selection == 4 ) {
setDirection(Directions.RIGHT);
}
//output
System.out.println("I have walked " + getDirection() + " " + steps + " steps.");
}
else if (input.nextLine().equalsIgnoreCase("N")) {
System.out.println("Exitting the system.");
System.exit(0);
}
}
public void play () {
Scanner input = new Scanner(System.in);
boolean valid = false;
int selection;
do {
System.out.println("How many times would you like to play?");
while (!input.hasNextInt()) {
System.out.println("That is not a number");
input.nextLine();
}
selection = input.nextInt();
valid = true;
} while(!valid);
for (int i = 1; i < selection + 1; i ++ ) {
if (getEnergy() >= energyRequired()) {
energyConsumption();
} else if (getEnergy() < energyRequired()) {
System.out.println("------------WHOOPS--------------.");
System.out.println("I do not have enough energy to play.");
regenerate();
}
}
//input.close();
}
public String toString() {
return "---------------------------------\nI AM AN ENTERTAINMENT ROBOT \nThe details of the entertainment robot are below: \n" +
"Name : " + getName() + "\nWeight: " + getWeight() + "\nHeight: " + getHeight() + "\nManufacturer: " +
getManufacturer() + "\nPurpose: " + getPurpose() + "\n----------------------------";
}
}
package Program;
import java.util.Scanner;
public class HumanStudyRobot extends Robot {
//instance variables
public HumanStudyRobot(String name, double height, double weight, String manufacturer) {
super(name, height, weight, manufacturer);
this.energy = 30.0;
}
@Override
public void start() {
System.out.println("This is a Human Study Robot");
System.out.println("The robot has started studying.");
}
@Override
public void stop() {
System.out.println("The robot has finished studying.");
}
@Override
public void doTask() {
study();
walk();
}
@Override
public void doTask(Scanner input) {
// TODO Auto-generated method stub
}
public void study() {
if (energy >= energyRequired()) {
energyConsumption();
}
else
if (energy < energyRequired()) {
System.out.println("The robot does not have sufficient energy.");
regenerate();
System.out.println("................");
System.out.println("The robot has finished regenerating");
}
}
public String toString() {
return "I AM A HUMAN STUDY ROBOT : \nThe details of the human study robot are below:\n"
+ "Name : " + getName() + "\nWeight: " + getWeight() + "\nHeight: "
+ getHeight() + "\nManufacturer : " + getManufacturer() + "\nPurpose : "
+ getPurpose();
}
public void walk() {
int steps;
boolean valid = false;
Scanner input = new Scanner(System.in);
System.out.println("Would you like me to walk for you?");
if (input.nextLine().equalsIgnoreCase("Y")){
do {
System.out.println("I can only walk 10 paces at a time");
System.out.println("For how many paces? ( Enter a no. between 1 and 10 )");
while (!input.hasNextInt()) {
System.out.println("That is not a number");
input.nextLine();
}
steps = input.nextInt();
valid = true;
} while(steps < 0 || steps > 10);
System.out.println("Which direction do you want me to walk? ( Enter a number between 1 and 4, the options are below.");
System.out.println("1 - " + Directions.FORWARD);
System.out.println("2 - " + Directions.BACKWARD);
System.out.println("3 - " + Directions.LEFT);
System.out.println("4 - " + Directions.RIGHT);
int selection = input.nextInt();
if (selection == 1 ) {
setDirection(Directions.FORWARD);
} else if (selection == 2 ) {
setDirection(Directions.BACKWARD);
} else if (selection == 3 ) {
setDirection(Directions.LEFT);
} else if (selection == 4 ) {
setDirection(Directions.RIGHT);
}
else System.out.println("That is not a valid selection.");
System.out.println("I have walked " + getDirection() + " " + steps + " steps");
}
}
}
答案 0 :(得分:0)
你没有以正确的方式编写foreach循环......
for (Robot eachRobot : Robots) { //for loop to go through the array list
System.out.println(Robots.toString());
System.out.println("\n");
}
这将打印整个列表元素:
ArrayList<Integer>lists=new ArrayList<Integer>();
lists.add(new Integer(50));
lists.add(new Integer(70));
for (Integer integer : lists) {
System.out.println(lists.toString());
}
将打印: [50,70] [50,70]
所以更改循环以打印每个元素toString函数它应该写成:
for (Robot eachRobot : Robots) { //for loop to go through the array list
System.out.println(eachRobot .toString()+"\n");
// println is already print new line for you if you want to print two lines you can use that instead.
}
重写循环并测试..