简体   繁体   English

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

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

I am a beginner, trying to make a basic hotel management system.我是一个初学者,正在尝试制作一个基本的酒店管理系统。

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

} } } }

I added a while loop so that the program never ends on its own and give user the authority to end the program.我添加了一个 while 循环,以便程序永远不会自行结束,并赋予用户结束程序的权限。

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

I want this program to take input again after the loop repeats and store the input in the same array with the previously added elements.我希望该程序在循环重复后再次获取输入,并将输入与先前添加的元素存储在同一个数组中。

Any suggestion will be appreciated任何建议将不胜感激

I am a beginner, trying to make a basic hotel management system.我是一个初学者,正在尝试制作一个基本的酒店管理系统。

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

} } } }

I added a while loop so that the program never ends on its own and give user the authority to end the program.我添加了一个 while 循环,以便程序永远不会自行结束,并赋予用户结束程序的权限。

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

I want this program to take input again after the loop repeats and store the input in the same array with the previously added elements.我希望该程序在循环重复后再次获取输入,并将输入与先前添加的元素存储在同一个数组中。

Any suggestion will be appreciated任何建议将不胜感激

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

相关问题 将用户输入多次存储在数组中 - Store user input in array multiple times Java JOptionPane:如何在字符串中获取用户输入并将其存储在数组中? - Java JOptionPane: How to take an user input in String and store it in an array? 从用户输入 1-10 次 5 次并将它们存储在一个数组中,如果输入错误,则提示输入正确 - Take input from user between 1-10 five times and store them in an array, if wrong input given then prompt for correct input 如何 output 从用户输入的数组列表中多次出现或列出的元素 - How to output an element that has occured or listed multiple times in an array list from a user input 如何获取用户输入的int数并同时存储用户的输入? - how to take the number of an int input from the user and at the same time store the user's input? 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used 如何将用户输入存储到数组列表中? - How do I store user input into an array list? 如何获取用户输入并将其存储到另一个类中的数组中? || 爪哇 - How do I take user input and store it into an array that is in another class? || Java 如何使用 java 在数组中获取用户输入? - how to take user input in Array using java? 如何在 JTextField 中多次捕获用户输入 - How to capture user input multiple times in a JTextField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM