我目前正在做这个项目:
创建一个简单的 Friends 类,至少包含以下内容:
-姓名和年龄字段
-适当的构造函数
-get/set 方法
-toString() 方法
创建朋友的 ArrayList。
编写一个程序来管理您的朋友列表。
从具有以下选项的菜单中运行程序:
-添加好友
-删除朋友
- 显示所有朋友
-出口
我已经走了一条路,但我不确定我是否朝着正确的方向前进。
这是我到目前为止所得到的:
import java.util.ArrayList;
import java.util.Scanner;
public class Friends
{
public static void main( String[] args )
{
int menu;
menu = 0;
int choice;
choice = 0;
Scanner input = new Scanner(System.in);
ArrayList< Friends > name = new ArrayList< >();
ArrayList< Friends > age = new ArrayList< >();
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
while(menu != 4)
{
switch(menu)
{
case 1:
while(choice != 2)
{
System.out.println("Enter Friend's Name: ");
name.add = input.next();
System.out.println("Enter Friend's Age: ");
age.add(input.nextInt());
System.out.println("Enter another? 1: Yes, 2: No");
choice = input.nextInt();
} break;
case 2:
System.out.println("Enter Friend's Name to Remove: ");
name.remove(input.next()); break;
case 3:
for(int i = 0; i < name.size(); i++)
{
System.out.println(name.get(i));
}
for(int k = 0; k < age.size(); k++)
{
System.out.println(age.get(k));
}break;
}
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
}
System.out.println("Thank you and goodbye!");
}
public String name;
public int age;
public Friends( String friendsName, int friendsAge )
{
this.name = friendsName;
this.age = friendsAge;
}
public String toString()
{
return super.toString();
}
public void setName( String friendsName )
{
name = friendsName;
}
public void setAge( int friendsAge )
{
age = friendsAge;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
我有几个问题:
- 如何使用 Friends 类来存储用户输入?(第 34 行和第 36 行有什么问题?)
当我显示朋友时,它会显示:
约翰·
詹
·杰夫
22
24
26
我希望将姓名和年龄放在一起,而不是全部放在一条线上。任何帮助将不胜感激!
这就是我现在所处的位置,但是我搞砸了一些事情,现在它不允许我在“FriendsTest f = new FriendsTest();”中提出论点,但是当我没有朋友列表时,只会说“空 0"
package friends;
import java.util.ArrayList;
import java.util.Scanner;
public class FriendsTest
{
public static void main( String[] args )
{
int menu;
int choice;
choice = 0;
Scanner input = new Scanner(System.in);
ArrayList< FriendsTest > friends = new ArrayList< >();
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
while(menu != 4)
{
switch(menu)
{
case 1:
while(choice != 2)
{
System.out.println("Enter Friend's Name: ");
String name = input.next();
System.out.println("Enter Friend's Age: ");
int age = input.nextInt();
FriendsTest f = new FriendsTest(name, age);
friends.add(f);
System.out.println("Enter another? 1: Yes, 2: No");
choice = input.nextInt();
} break;
case 2:
System.out.println("Enter Friend's Name to Remove: ");
friends.remove(input.next()); break;
case 3:
for(int i = 0; i < friends.size(); i++)
{
System.out.println(friends.get(i).name + " " + friends.get(i).age);
}
break;
}
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
}
System.out.println("Thank you and goodbye!");
}
public String name;
public int age;
}