Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有菜单方法的Java arraylist_Java_Eclipse_Arraylist_Menu_Java.util.scanner - Fatal编程技术网

使用带有菜单方法的Java arraylist

使用带有菜单方法的Java arraylist,java,eclipse,arraylist,menu,java.util.scanner,Java,Eclipse,Arraylist,Menu,Java.util.scanner,此代码有两个问题: 我该如何进行编码,以便在用户将“theFruit”添加到arraylist后,如果他们按“V”查看所有水果,它将包括默认水果以及他们添加的水果 同样在“AddFruit”方法中,它只允许我在打印前添加2个水果。为什么会这样?先谢谢你 import java.util.ArrayList; import java.util.Scanner; public class StkOv { public static void main(String[] args) {

此代码有两个问题: 我该如何进行编码,以便在用户将“theFruit”添加到arraylist后,如果他们按“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);
    }


}
}
import java.util.ArrayList;
导入java.util.Scanner;
公共级StkOv{
公共静态void main(字符串[]args){
菜单();
}
公共静态无效菜单()
{
字符串Customer[]=新字符串[10];
扫描仪输入=新扫描仪(System.in);
字符串选项;
只要没有选择Q,就执行{//循环回到这里
System.out.println(“\n菜单”);
System.out.println(“V:查看所有水果”);
System.out.println(“A:将水果添加到列表中”);
System.out.println(“Q:退出”);
option=input.next();
if(option.charAt(0)='V')
{ 
viewalfruit(客户);
} 
if(option.charAt(0)='A')
{ 
AddFruit(客户);
}
}
while(option.charAt(0)!='Q');
}
公共静态void viewAllFruit(字符串CustomerRef[])
{
ArrayList theFruit=新的ArrayList();
水果。添加(“李子”);
水果。添加(“葡萄”);
水果。添加(“橙子”);
果实。添加(“李子”);
水果。添加(“苹果”);
int size=theFruit.size();

对于(int x=0;x可能有两个以上:)

您的水果列表在您的程序中应该是“全局”的,这样它的内容就可以通过viewAllFruit打印出来,并且新的水果可以添加到addFruit方法中

现在的情况是,在这两种方法中,您都可以创建新列表,向其中添加“defualt”水果,然后打印内容。当用户输入其选择时,您将再次创建这些列表

请记住永远不要将字符串与==或!=始终与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');

}
public静态无效列表initFruits(){
ArrayList theFruit=新的ArrayList();
水果。添加(“李子”);
水果。添加(“葡萄”);
水果。添加(“橙子”);
果实。添加(“李子”);
水果。添加(“苹果”);
归还果实;
}
公共静态void viewAllFruit(列出水果,字符串CustomerRef[])
{
用于(串水果:水果){
System.out.println(“水果”+水果+”有库存);
}
}
公共静态void addFruit(列出水果,字符串CustomerRef[])
{
扫描仪输入=新扫描仪(System.in);
String EE=“EE”//您认为EE是退出,但您想与“再见!”进行比较;
while(true){
System.out.println(“输入您想要添加的水果,然后EE退出”);
字符串选择=input.next();
if(选择等于(EE)){
打破
};
添加(选择);
}
}
公共静态无效菜单()
{
字符串Customer[]=新字符串[10];
扫描仪输入=新扫描仪(System.in);
列出水果=initFruits();
字符串选项;
只要没有选择Q,就执行{//循环回到这里
System.out.println(“\n菜单”);
System.out.println(“V:查看所有水果”);
System.out.println(“A:将水果添加到列表中”);
System.out.println(“Q:退出”);
option=input.next();
if(option.charAt(0)='V')
{ 
查看所有水果(水果、客户);
} 
if(option.charAt(0)='A')
{ 
addFruit(水果、客户);
}
}
while(option.charAt(0)!='Q');
}

非常感谢@Zielu非常感谢你。肯定会做更多的研究。再次感谢!