我的程序正在做一些奇怪的事情

时间:2015-10-27 22:46:05

标签: java eclipse

所以这是我的任务目标:

  

假设所有员工的每周工作时间都存储在一个   二维数组。每行记录员工的n天工作时间   n列,其中n≥1,n≤7表示天数   在这些员工工作的一周内。例如,显示的表格   下面表示一个存储8个工时的数组   员工一周7天。写一个接收的程序   投入,员工人数和工作日数   周。然后它接收所有员工信息(姓名和号码)   每天工作时间)。该程序应显示员工和   他们的总工作时间在一周内按总数递减   小时。

输出应该如下所示:

Employee 7 worked 39 hours
Employee 6 worked 37 hours
Employee 0 worked 34 hours
Employee 4 worked 32 hours
Employee 3 worked 31 hours
Employee 1 worked 28 hours
Employee 5 worked 28 hours
Employee 2 worked 24 hours

此外,输出应按照大多数工​​作时间的降序排列至少......

我的代码很奇怪,在我的输出中给了我“null”,它也没有显示我把它们放进去后每个员工工作了多少小时......

这是我的代码:

package assignment3TL;

import java.util.*;

public class EmployeeWorkHours {

    public static void main(String[] args) {

        Scanner turtle = new Scanner(System.in);
        String[] calender = {"S", "M", "T", "W", "Th", "F", "S"};
        System.out.println("How many Employee's do you have?: ");
        int NUMBER_OF_EMPLOYEES = turtle.nextInt();
        turtle.nextLine();
        int[][] hours;
        hours = new int[NUMBER_OF_EMPLOYEES][7];
        String[][] employee = new String[NUMBER_OF_EMPLOYEES][2];

        // input for Names
        for (int x = 0; x < (employee.length); x++) {
            System.out.println("Name of Employee " + (x + 1) + ": ");
            String name = turtle.nextLine();
            employee[x][1] = name;

        }
        // input for Hours
        for (int z = 0; z < hours.length; z++) {
            System.out.println("Starting from Sunday, enter the hours Employee " + (z + 1) + " has worked each day (Make sure you seperate it by spaces): ");
            for (int a = 0; a < (hours[0].length); a++) {
                hours[z][a] = turtle.nextInt();
            }
        }
        // Print everything out
        for (int i = 0; i < employee.length; i++) {
            for (int z = 0; z < employee[0].length; z++) {
                System.out.println(employee[i][z] + "-");
            }
            for (int f = 0; f < NUMBER_OF_EMPLOYEES; f++) {

                System.out.println(" " + hours[i][f]);

            }
        }
        // Total hours.
        for (int s = 0; s < hours[0].length; s++) {
            int counter = 0;
            for (int d = 0; d < hours.length; d++) {
                hours[d][s] += counter;
            }
            System.out.println("Employee " + (s + 1) + " worked " + counter + "   hours.");
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您当前的代码存在多处问题。除了捕获nextlineemployee的二维数组问题之外,打印所需输出的整个逻辑是错误的。 for循环条件也完全错误,无法获得所需的输出。所以程序没有做任何奇怪的事情 - 你只是编错了。

你应该尝试这个更新的代码:

      Scanner turtle = new Scanner(System.in);
      String[] calender = { "S", "M", "T", "W", "Th", "F", "S" };
      System.out.println("How many Employee's do you have?: ");
      int NUMBER_OF_EMPLOYEES = turtle.nextInt();
      turtle.nextLine();

      int [][]hours;
      int []totalHours= new int[NUMBER_OF_EMPLOYEES];

      hours = new int[NUMBER_OF_EMPLOYEES][7];
      String[] employee = new String[NUMBER_OF_EMPLOYEES];

      // input for Names
      for (int x = 0; x < (employee.length); x++) {
          System.out.println("Name of Employee " + (x + 1) + ": ");
          String name = turtle.nextLine();
          employee[x] = name;

      }
      // input for Hours
      for (int z = 0; z < employee.length; z++) {
          System.out.println("Starting from Sunday, enter the hours Employee "+ (z + 1)+ " has worked each day (Make sure you seperate it by spaces): ");
          for (int a = 0; a < 7; a++) {
              hours[z][a] = turtle.nextInt();
          }
          turtle.nextLine();
     }
      // Print everything out
      for (int i = 0; i < employee.length; i++) {
          totalHours[i]=0;
          for(int j=0; j<7; j ++){
              totalHours[i] = totalHours[i]+hours[i][j];   
          }

        System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");  

      }

另请注意,我添加了totalHours数组并使用它来获取最终输出。

希望有所帮助