使用带有菜单方法的 Java arraylist
Using a Java arraylist with a menu method
此代码有 2 个问题:
我该如何编码,以便在用户添加到数组列表后 'theFruit' 如果他们然后按 'V' 查看所有水果,它包括默认水果以及他们添加的水果?
同样在方法'AddFruit'中它只允许我在打印前添加2个水果。为什么是这样?提前谢谢你
import java.util.ArrayList;
import java.util.Scanner;
public class StkOv {
public static void main(String[] args) {
TheMenu();
}
public static void TheMenu()
{
String Customer[] = new String[10];
Scanner input = new Scanner(System.in);
String option;
do { // loop back to here as long as Q isn't selected
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("A: To add a fruit to the list");
System.out.println("Q: To exit");
option = input.next();
if (option.charAt(0) == 'V' )
{
viewAllFruit(Customer);
}
if (option.charAt(0) == 'A' )
{
AddFruit(Customer);
}
}
while (option.charAt(0) != 'Q');
}
public static void viewAllFruit(String CustomerRef[])
{
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
int size = theFruit.size();
for (int x = 0; x<size; x++) //for loop for fruits
{
System.out.println("Fruit " + x + " is in stock " + theFruit);
}
}
public static void AddFruit(String CustomerRef[])
{
Scanner input = new Scanner(System.in);
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
System.out.println("Enter the fruits you'd like to add and EE to exit");
String choice = input.next();
String EE = "Goodbye!";
if (choice !=EE);
{
theFruit.add(choice);
choice = input.next();
}
for (String S : theFruit)
{
System.out.println(S);
}
}
}
可能不止两个:)
你的水果列表应该在你的程序中"global",这样它的内容就可以被viewAllFruit打印出来,新的水果可以在addFruit方法中添加。
就像现在一样,在创建新列表的两种方法中,向其中添加 "defualt" 水果,然后打印内容。当用户输入其选择时,您将再次创建这些列表。
记住 永远不要用 == 或 != 比较字符串,总是用 equals()
下面是您的代码的一些 "cleaner" 版本。
public static void List<String> initFruits() {
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
return theFruit;
}
public static void viewAllFruit(List<String> fruits,String CustomerRef[])
{
for (String fruit : fruits) {
System.out.println("Fruit " + fruit + " is in stock ");
}
}
public static void addFruit(List<String> fruits,String CustomerRef[])
{
Scanner input = new Scanner(System.in);
String EE = "EE"; //you assked for EE as quit but you wanted to compare with "Goodbye!";
while(true) {
System.out.println("Enter the fruits you'd like to add and EE to exit");
String choice = input.next();
if (choice.equals(EE)) {
break;
};
fruits.add(choice);
}
}
public static void TheMenu()
{
String Customer[] = new String[10];
Scanner input = new Scanner(System.in);
List<String> fruits = initFruits();
String option;
do { // loop back to here as long as Q isn't selected
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("A: To add a fruit to the list");
System.out.println("Q: To exit");
option = input.next();
if (option.charAt(0) == 'V' )
{
viewAllFruit(fruits,Customer);
}
if (option.charAt(0) == 'A' )
{
addFruit(fruits,Customer);
}
}
while (option.charAt(0) != 'Q');
}
此代码有 2 个问题: 我该如何编码,以便在用户添加到数组列表后 'theFruit' 如果他们然后按 'V' 查看所有水果,它包括默认水果以及他们添加的水果?
同样在方法'AddFruit'中它只允许我在打印前添加2个水果。为什么是这样?提前谢谢你
import java.util.ArrayList;
import java.util.Scanner;
public class StkOv {
public static void main(String[] args) {
TheMenu();
}
public static void TheMenu()
{
String Customer[] = new String[10];
Scanner input = new Scanner(System.in);
String option;
do { // loop back to here as long as Q isn't selected
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("A: To add a fruit to the list");
System.out.println("Q: To exit");
option = input.next();
if (option.charAt(0) == 'V' )
{
viewAllFruit(Customer);
}
if (option.charAt(0) == 'A' )
{
AddFruit(Customer);
}
}
while (option.charAt(0) != 'Q');
}
public static void viewAllFruit(String CustomerRef[])
{
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
int size = theFruit.size();
for (int x = 0; x<size; x++) //for loop for fruits
{
System.out.println("Fruit " + x + " is in stock " + theFruit);
}
}
public static void AddFruit(String CustomerRef[])
{
Scanner input = new Scanner(System.in);
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
System.out.println("Enter the fruits you'd like to add and EE to exit");
String choice = input.next();
String EE = "Goodbye!";
if (choice !=EE);
{
theFruit.add(choice);
choice = input.next();
}
for (String S : theFruit)
{
System.out.println(S);
}
}
}
可能不止两个:)
你的水果列表应该在你的程序中"global",这样它的内容就可以被viewAllFruit打印出来,新的水果可以在addFruit方法中添加。
就像现在一样,在创建新列表的两种方法中,向其中添加 "defualt" 水果,然后打印内容。当用户输入其选择时,您将再次创建这些列表。
记住 永远不要用 == 或 != 比较字符串,总是用 equals()
下面是您的代码的一些 "cleaner" 版本。
public static void List<String> initFruits() {
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
return theFruit;
}
public static void viewAllFruit(List<String> fruits,String CustomerRef[])
{
for (String fruit : fruits) {
System.out.println("Fruit " + fruit + " is in stock ");
}
}
public static void addFruit(List<String> fruits,String CustomerRef[])
{
Scanner input = new Scanner(System.in);
String EE = "EE"; //you assked for EE as quit but you wanted to compare with "Goodbye!";
while(true) {
System.out.println("Enter the fruits you'd like to add and EE to exit");
String choice = input.next();
if (choice.equals(EE)) {
break;
};
fruits.add(choice);
}
}
public static void TheMenu()
{
String Customer[] = new String[10];
Scanner input = new Scanner(System.in);
List<String> fruits = initFruits();
String option;
do { // loop back to here as long as Q isn't selected
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("A: To add a fruit to the list");
System.out.println("Q: To exit");
option = input.next();
if (option.charAt(0) == 'V' )
{
viewAllFruit(fruits,Customer);
}
if (option.charAt(0) == 'A' )
{
addFruit(fruits,Customer);
}
}
while (option.charAt(0) != 'Q');
}