Java 如何使用循环将数据输入数组?

Java 如何使用循环将数据输入数组?,java,arrays,loops,Java,Arrays,Loops,我试图制作一个简单的程序,用户将员工信息输入到一个数据库中,该数据库可容纳五名员工;包括姓名、年龄、工资和自动存储的ID。然而,我似乎遇到了麻烦,无法让循环正常工作,在第一个数组之后遇到了问题。我怀疑for循环中存在问题。有人能帮我吗?代码如下: package database; import java.util.Scanner; /** * * @author Justin */ public class employees { public static void main

我试图制作一个简单的程序,用户将员工信息输入到一个数据库中,该数据库可容纳五名员工;包括姓名、年龄、工资和自动存储的ID。然而,我似乎遇到了麻烦,无法让循环正常工作,在第一个数组之后遇到了问题。我怀疑for循环中存在问题。有人能帮我吗?代码如下:

package database;

import java.util.Scanner;
/**
 *
 * @author Justin
 */
public class employees {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String[] names;
        names = new String[4];
        int[] id;
        id = new int[4];
        double[] wage;
        wage = new double[4];
        int[] age;
        age = new int[4];
        System.out.println("This program is designed to store data for 5 employees.\nEnter data for 5 employees\n");

        for (int count = 0; count <= 4; count++){
            //Used to enter data for each employee
            id[count] = count;
            System.out.println("Enter the name of employee number " + id[count]);
            names[count] = scan.nextLine();
            System.out.println("Enter the wage of " + names[count]);
            wage[count] = scan.nextDouble();
            System.out.println("Enter the age of " + names[count]);
            age[count] = scan.nextInt();
        }

        System.out.println("All employees are now in the database.");
        System.out.println("Enter the name or ID number of an employee to access their data");

    }
}
包数据库;
导入java.util.Scanner;
/**
*
*@作者贾斯汀
*/
公营雇员{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
字符串[]名称;
名称=新字符串[4];
int[]id;
id=新整数[4];
双倍工资;
工资=新的双倍[4];
int[]年龄;
年龄=新整数[4];
System.out.println(“此程序设计用于存储5名员工的数据。\n输入5名员工的数据\n”);

对于(int count=0;count,您试图在每个数组中存储5个值,但数组已初始化为仅包含4个元素

例如,这将创建一个
字符串
数组,该数组最多可存储4个元素:

String[] names = new String[4];
如果需要存储5个元素,则应执行以下操作:

String[] names = new String[5];
通过使用变量指定要存储的员工数量,可以避免此类错误:

int numEmployees = 5;
String[] names = new String[numEmployees];
int[] id = new int[numEmployees];
double[] wage = new double[numEmployees];
int[] age = new int[numEmployees];

for (int count = 0; count < numEmployees; count++) {
    // read and store employee information
}
并将信息存储在
列表中

Scanner scan=新的扫描仪(System.in);
int numEmployees=5;//也可以从输入中读取
List employees=new ArrayList();
对于(int count=0;count
简短回答: 循环首先进入“declaredVariables”区域一次,但不超过一次

然后它就这样开始了:

while ( iterationCondition == true ){
    // invoke codeBlock
    // invoke postIterationAction 
}
一旦iterationCondition返回false,它就会停止


因此,当您的计数被声明为0时,您的条件是count您确实应该创建一个Employee类并创建一个Employee的ArrayList。如果您使用数组来构建业务算法,您就剥夺了现代编程的便利性和最佳实践。

您的代码有两个问题: 第一个是对5个元素进行数组声明。 第二个是使用
scan.nextLine()
将其更改为
scan.line()
scan.readLine()
。这将解决您的问题。 因为对nextLine()的调用首先结束了用户输入第一个员工年龄的那一行。为什么?因为nextLine()只读取一个int,而不结束这一行

现在,您的代码如下所示:

声明中

 String[] names;
 names = new String[5];
 int[] id;
 id = new int[5];
 double[] wage;
 wage = new double[5];
 int[] age;
 age = new int[5];
在阅读代码时,请更改以下内容:

 System.out.println("Enter the name of employee number " + id[count]);
 names[count] = scan.nextLine();
致:


谢谢。我似乎一直遇到同样的问题,但是,当我开始输入第二个数组的数据时,程序会说:“输入员工姓名2输入工资”谢谢,当我尝试多个类时,这会很有帮助谢谢;这很有帮助这解决了我的问题,我的新手可以开始理解。但是,我仍然对“下一个”和“下一个”之间的区别有点困惑,但我可以查一下。当然,谢谢你,只是说有更简单、更容易获得的方法。
count <= 4
count < 4
for( declaredVariables ; iterationCondition ; postIterationAction ){
    codeBlock
}
while ( iterationCondition == true ){
    // invoke codeBlock
    // invoke postIterationAction 
}
1) count = 0
2) count = 1
3) count = 2
4) count = 3
5) count = 4
 String[] names;
 names = new String[5];
 int[] id;
 id = new int[5];
 double[] wage;
 wage = new double[5];
 int[] age;
 age = new int[5];
 System.out.println("Enter the name of employee number " + id[count]);
 names[count] = scan.nextLine();
 System.out.println("Enter the name of employee number " + id[count]);
 names[count] = scan.next();