繁体   English   中英

如何多次获取用户输入并将其存储在同一个数组列表中?

[英]How to take user input multiple times and store it in the same array list?

我是一个初学者,正在尝试制作一个基本的酒店管理系统。

    Scanner sc = new Scanner(System.in);
System.out.print("Number of guests: ");
int no = sc.nextInt();
String[] arr = new String[no];

while(true)
{
  Scanner input = new Scanner(System.in);
  System.out.println("Welcome, How may I help you");
  String req = input.nextLine();

  if(req.equals("register"))
  {
    System.out.println("Enter name:");

    for(int i=0;i<no;i++)
    {
      arr[i] = input.nextLine();
    }
  }
  else if(req.equals("check"))
  {
    Scanner out = new Scanner(System.in);
    System.out.print("Search name: ");
    String ser = out.nextLine();
    int log=0;

    for(int j=0;j<no;j++)
    {
      if(arr[j].equals(ser))
      {
        System.out.println(ser + " is present");
        log++;
      }
    }
    if(log==0)
    {
      System.out.println(ser + " is absent");
    }
  }
  else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }
}

} }

我添加了一个 while 循环,以便程序永远不会自行结束,并赋予用户结束程序的权限。

       else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }

我希望该程序在循环重复后再次获取输入,并将输入与先前添加的元素存储在同一个数组中。

任何建议将不胜感激

我是一个初学者,正在尝试制作一个基本的酒店管理系统。

    Scanner sc = new Scanner(System.in);
System.out.print("Number of guests: ");
int no = sc.nextInt();
String[] arr = new String[no];

while(true)
{
  Scanner input = new Scanner(System.in);
  System.out.println("Welcome, How may I help you");
  String req = input.nextLine();

  if(req.equals("register"))
  {
    System.out.println("Enter name:");

    for(int i=0;i<no;i++)
    {
      arr[i] = input.nextLine();
    }
  }
  else if(req.equals("check"))
  {
    Scanner out = new Scanner(System.in);
    System.out.print("Search name: ");
    String ser = out.nextLine();
    int log=0;

    for(int j=0;j<no;j++)
    {
      if(arr[j].equals(ser))
      {
        System.out.println(ser + " is present");
        log++;
      }
    }
    if(log==0)
    {
      System.out.println(ser + " is absent");
    }
  }
  else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }
}

} }

我添加了一个 while 循环,以便程序永远不会自行结束,并赋予用户结束程序的权限。

       else if(req.equals("exit"))
  {
    System.out.println("\nBye...\n");
    System.exit(0);
  }

我希望该程序在循环重复后再次获取输入,并将输入与先前添加的元素存储在同一个数组中。

任何建议将不胜感激

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM