【发布时间】:2015-04-14 00:59:08
【问题描述】:
首先,如果我做错了什么,我想道歉,因为这是我的第一篇文章,我对 Java 非常缺乏经验。我想知道如何在不弹出类似“Friend@8410b1”之类的内容的情况下打印这个名为“addressBook”的 LinkedList。另外,我怎样才能循环这个 if 语句?
import java.io.*;
import java.util.*;
public class ABook
{
public static void main (String args[])
{
LinkedList addressBook = new LinkedList();
Scanner input = new Scanner(System.in);
System.out.println("Would you like to add a friend? (Say Y or N)");
String reply = input.nextLine();
if(reply.equals("Y"))
{
System.out.println("What is the name of your friend?");
String name = input.nextLine();
System.out.println("What is the age of your friend?");
int age = input.nextInt();
Friend newFriend = new Friend(name,age);
addressBook.add(newFriend);
System.out.println("This is your Address Book so far: " + addressBook);
}
else if(reply.equals("N")){
System.out.println("Thank you for your time");
}
}
}
【问题讨论】:
-
这篇关于堆栈溢出的帖子应该对你有帮助stackoverflow.com/questions/19283083/…
标签: java object linked-list