2015-04-12 68 views
-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); 
    } 


} 
} 
+0

非常感謝你@Zielu認真地不能感謝你。肯定會做更多的研究。再次感謝! –

回答

0

可能有兩個以上的:)

你的水果名單應該在你的節目「全球」因此它的內容可以通過viewAllFruit打印,並且可以添加新果在addFruit方法中。

現在,在兩種方法中,您都會創建新列表,然後添加「defualt」結果,然後打印內容。當用戶進入其選擇時,您再次創建這些列表。

記住NEVER比較==或!=總是與equals()方法的字符串

下面是你的代碼的一些 「乾淨」 版本。

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'); 

}