-1
此代碼的兩個問題: 如何編碼此代碼,以便在用戶添加到數組列表'theFruit'之後,如果他們按'V'查看所有包含默認水果以及他們添加的水果?使用帶菜單方法的Java ArrayList
同樣在「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);
}
}
}
非常感謝你@Zielu認真地不能感謝你。肯定會做更多的研究。再次感謝! –