Java 对于每个循环,使用toString从arrayList打印详细信息

Java 对于每个循环,使用toString从arrayList打印详细信息,java,loops,object,element,tostring,Java,Loops,Object,Element,Tostring,我被要求:  创建娱乐机器人对象和HumanStudyRobot对象(使用前面的数据 并将这些对象存储在数组列表中。  使用本文前面概述的数据设置阵列中每个机器人的用途。  使用toString方法打印每个机器人的详细信息 我已经这样做了,我的代码如下。但是当我运行代码时,添加到arrayList中的每个机器人的详细信息都会打印两次。你知道我如何修复它,使数组列表中每个机器人对象的细节只打印一次吗?非常感谢 package Program; import java.util.ArrayL

我被要求:  创建娱乐机器人对象和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");
        }

    }

}
打包程序;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类测试员04{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
ArrayList Robots=新的ArrayList();
HumanStudyRobot HumanStudyRobot1=新的HumanStudyRobot(“HRP”,1.5,58.0,“川田工业”);
HumanStudyRobot1.setPurpose(“研究人类运动并执行广泛的任务”);
娱乐机器人娱乐机器人1=新的娱乐机器人(“QRIO”,0.6,7.3,“索尼”);
娱乐机器人1.设定目标(“与你一起生活,让生活充满乐趣,让你快乐。”);
机器人。添加(娱乐机器人1);
添加(HumanStudyRobot1);
for(Robot-eachRobot:Robots){//for循环遍历数组列表
System.out.println(Robots.toString());
System.out.println(“\n”);
}
startRobot(机器人,输入);
}
公共静态void startRobot(ArrayList机器人、扫描仪输入){
对于(int i=0;i=energyRequired()){
能量消耗();
}else if(getEnergy()  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());
        }
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.
 }