![](/img/trans.png)
[英]Printing an Arraylist in toString with lines separating each entry
[英]for each loop printing details from an arrayList using toString
我被要求執行以下操作:創建一個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");
}
}
}
您沒有以正確的方式編寫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.
}
重寫循環並測試..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.