[初级]获取和错误以及(加上对为什么代码功能不正确感到困惑)#java



计算每个员工每周工作时间:

假设员工每周工作时间存储在一个数组中。它用7个元素记录了员工7天的工作时间。例如,我们存储八名员工的工作时间。编写一个程序,显示员工和他们的总工作时间,按总工作时间递减的顺序排列。输入例子:

               S  M  Tu W  Th F  S
Employee1-Paul 0  8  8  8  8  8  0
Employee2-Mary 4  8  8  5  8  0  2
Employee3-Yin  5  8  8  2  8  0  1

打印总数:

Paul: 40 hours.
Mary: 35 hours.
Yin: 32 hours.

这是我的作业的问题,但我得到2个逻辑错误。这是我的代码。

import java.util.*;
    public class employeeHours {
    public static void main(String[] arges) {
        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)+ " have worked (Make sure you seperate it by spaces): ");
            for (int a = 0; z < (hours[0].length); a++) {
                hours[z][a] = turtle.nextInt();
            }
        }
        // Print everything out
        for (int i = 0; i < employee.length; i++) {
            for (int z = 0; i <= employee[0].length; z++) {
                System.out.print(employee[i][z] + "-");
            }
            for (int f = 0; f < NUMBER_OF_EMPLOYEES; f++) {
                System.out.print(" " + 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) + ":" + counter + " Hours");
        }
    }
}

好了,当我运行这段代码。它会问我有多少员工。我以2为例。假设要查询雇员1的姓名,我输入姓名,然后输出输入雇员2的姓名。相反,它会同时输出"输入员工1的姓名"one_answers"输入员工2的姓名"。

看!

你有多少雇员?:
2
员工1姓名:
员工2姓名:

OK现在我得到java.lang. arrayindexoutofboundexception的另一个错误。

你有多少雇员?:
2
员工1姓名:
嫁给
员工2姓名:
保罗输入员工1的工作时数(确保用空格分隔):
1
1
1
1
1
1
1
1
1
线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException: 7在employeeHours.main (employeeHours.java: 24)????

使用nextInt()有一个问题。

如果你在经典的用户输入中使用它,它接受整数,但是它在扫描器中左行结束。所以当你使用nextLine时,它会找到行尾并"跳过它"。

最简单的解决方法是在使用后"冲洗":

    int NUMBER_OF_EMPLOYEES = turtle.nextInt();
    turtle.nextLine();

如果你拼错了正确的变量,改变这一行:

for (int a = 0; z < (hours[0].length); a++) {

to this:

for (int a = 0; a < (hours[0].length); a++) {


和最后一个错误

这条线

for (int z = 0; i <= employee[0].length; z++) {

应改为:

for (int z = 0; z < employee[0].length; z++) {

可运行代码:

public static void main(String[] arges) {
    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)+ " have worked (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.print(employee[i][z] + "-");
        }
        for (int f = 0; f < NUMBER_OF_EMPLOYEES; f++) {
            System.out.print(" " + 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) + ":" + counter + " Hours");
    }
}

示例输出:

How many Employee's do you have?: 
1
Name of Employee 1: 
libik
Starting from Sunday Enter the hours Employee 1 have worked (Make sure you seperate it by spaces): 
10 20 30 40 50 60 70
null-libik- 10Employee 1:0 Hours
Employee 2:0 Hours
Employee 3:0 Hours
Employee 4:0 Hours
Employee 5:0 Hours
Employee 6:0 Hours
Employee 7:0 Hours

相关内容

  • 没有找到相关文章

最新更新