[英]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.