Java 如何在代码运行时向数组添加随机生成的字符串和整数?
我正在尝试制作一个基于文本的商业游戏,在那里你可以雇佣和解雇员工。最多可以有5名员工,一旦达到5名,我们可能会解雇员工。我已经编写了随机生成新申请者详细信息的代码,但我无法保存雇用的员工。有人能给我引路吗Java 如何在代码运行时向数组添加随机生成的字符串和整数?,java,arrays,eclipse,Java,Arrays,Eclipse,我正在尝试制作一个基于文本的商业游戏,在那里你可以雇佣和解雇员工。最多可以有5名员工,一旦达到5名,我们可能会解雇员工。我已经编写了随机生成新申请者详细信息的代码,但我无法保存雇用的员工。有人能给我引路吗 package TestPackage; import java.util.*; public class EmployeeRandomizer { /*this is where the employee's profile will be created and chosen
package TestPackage;
import java.util.*;
public class EmployeeRandomizer {
/*this is where the employee's profile will be created and chosen by user. */
public static void main (String args[]) {
int spacesAvailable = 5;
int employeesHired = 0;
while(true) {
Random generate = new Random();
String[] name = {"John", "Marcus", "Susan", "Henry"};
int[] age = {20,21,22,23,24,25,25,26,26,27,28,29,30,20,21,22,23,24,25,25,26,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
String[] education = {"business", "accountant", "human resources","none"};
for(int i = 0; i < employeesHired; i++){
int index = (int)(Math.random() * 10);
System.out.println(name[index]+age[index]+education[index]);
}
System.out.println("name: " +name[generate.nextInt(4)]);
System.out.println("age: "+age[generate.nextInt(40)]);
System.out.println("education: " +education[generate.nextInt(4)]);
System.out.println("will you hire this person? (y/n)");
Scanner hire = new Scanner(System.in);
String hireEmployee = hire.next();
if(hireEmployee.equals("y")) {
System.out.println("you have hired a new employee!");
spacesAvailable = spacesAvailable-1;
employeesHired = employeesHired+1;
System.out.println("employees: " + employeesHired);
if(spacesAvailable>0 && employeesHired<5) {
System.out.println("There's " +spacesAvailable+" available spaces in your employee list.");
System.out.println("1. hire more employees");
System.out.println("2. view employees list");
Scanner hireMore = new Scanner(System.in);
String hireMoreAnswer =hireMore.next();
if (hireMoreAnswer.equals("1")) {
continue;
}
if (hireMoreAnswer.equals("2")) {
for(int i = 0; i < employeesHired;){
int index = (int)(Math.random() * 10);
System.out.println(name[index]);
System.out.println(age[index]);
System.out.println(education[index]);
break;
}
break;
}
}
if(spacesAvailable==0 || employeesHired==5) {
System.out.println("You have no more spaces left to hire more employees. (1 or 2?)");
System.out.println("1. Fire an employee (+1 space)");
System.out.println("2. Exit (more coming soon!)");
Scanner oneTwo = new Scanner(System.in);
String oneOrTwo = oneTwo.next();
if(oneOrTwo.equals("1")) {
}
if (oneOrTwo.contentEquals("2")) {
break;
}
}
}
if(hireEmployee.equals("n")) {
System.out.println("you have declined this application");
System.out.println("There's " +spacesAvailable+" available spaces in your employee list. Would you like to hire more employees? (y/n)");
Scanner hireMoreTwo = new Scanner(System.in);
String hireMoreAnswerTwo =hireMoreTwo.next();
if (hireMoreAnswerTwo.equals("y")) {
continue;
}
if (hireMoreAnswerTwo.equals("n")) {
break;
}
}
}
}
}
包TestPackage;
导入java.util.*;
公共类EmployeeRandomizer{
/*这是用户创建和选择员工档案的地方*/
公共静态void main(字符串参数[]){
int spacesavaailable=5;
int employeesHired=0;
while(true){
随机生成=新随机();
字符串[]名称={“约翰”、“马库斯”、“苏珊”、“亨利”};
int[]年龄={20,21,22,23,24,25,25,26,26,27,28,29,30,20,21,22,23,24,25,26,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,48,49,50};
字符串[]教育={“商业”、“会计”、“人力资源”、“无”};
for(int i=0;i if(spacesavaailable>0&&employeeshired为什么在大多数if
语句中都有一个break
?您甚至在一个for
循环中都有它,因此它基本上不再是一个循环,因为每次都会立即中断。此外,您不需要声明一个新扫描仪(System.in)
每次接受输入时,只需使用前面声明的同一个。最后,您应该研究如何使用ArrayList
来存储信息,例如您提到的应用程序。理想情况下,您可以创建一个名为application
的自定义类或类似的类,并创建一个ArrayList
。您想将这些随机生成的字符串和整数添加到哪些数组中?我没有看到任何数组是空的、准备好的、等待被雇佣员工的数据填充的。我从最初的测试版开始就编写java代码,而且我从来没有使用过中断或继续。我在这里看不到使用它们的好理由。我使用了中断,然后继续我还没有完成这个项目,因为雇佣和解雇只是其中的一部分。