制作可接受姓名,小时,付费的并行数组

时间:2018-05-02 17:45:05

标签: java arrays

所以对于这个程序我需要有三个数组接受数据并显示总薪水,它似乎首先工作但是在输入第一个人的数据后,我的代码开始堆叠在窗口的顶部,请帮忙我解决这个问题才能正常工作。

我需要使用数组。不允许使用对象。

import java.util.Scanner;

public class ThreeArrays {

    public static void main(String args[]) {
        float[] payRate = new float[5];
        float[] hours = new float[5];
        String[] name = new String[5];

        getPayData(name, hours, payRate);

        displayGrossPay(name, hours, payRate);
    }

    public static void getPayData(String[] name, float[] hours, float[] payRate) {
        Scanner kb = new Scanner(System.in);

        for (int i = 0; i < hours.length; i++) {

            System.out.print("Enter the employee's name: ");
            name[i] = kb.nextLine();

            System.out.print("Enter the employee's hours: ");
            hours[i] = kb.nextFloat();

            System.out.print("Enter the employee's hourly rate: ");
            payRate[i] = kb.nextFloat();
        }

    }

    public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
        for (int i = 0; i < hours.length; i++) {
            System.out.println("Employee name: " + name[i] + " Gross Pay: " + hours[i] *
                payRate[i]);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

System.out.println();放在要求数据的每个语句之间。

编辑:也可能是\n字符未被.nextInt()读取。要解决此问题,请在kb.nextLine();

之后添加kb.nextFloat();
    for (int i = 0; i < hours.length; i++) {

        System.out.print("Enter the employee's name: ");
        name[i] = kb.nextLine();

        System.out.println("");

        System.out.print("Enter the employee's hours: ");
        hours[i] = kb.nextFloat();
        kb.nextLine();
        System.out.println("");

        System.out.print("Enter the employee's hourly rate: ");
        payRate[i] = kb.nextFloat();
        kb.nextLine();
        System.out.println("");

    }

    // Enter the employee's name: Foo
    // (Blank Line)
    // Enter the employee's hours: 1
    // (Blank Line)
    // Enter the employee's hourly rate: -20.00 // They owe me now. :D
    // (Blank Line)

答案 1 :(得分:0)

问题与 stdin flushing 有关。因为行payRate[i] = kb.nextFloat();的上次输入浮点值不会删除\n个字符。在下一次迭代中,行\n将提取此name[i] = kb.nextLine();字符。这进一步为行String留下hours[i] = kb.nextFloat();值。由于它不是有效的float,因此您将获得例外:java.util.InputMismatchException

要解决此问题,请在第if(i < hours.length-1) kb.next();行之后使用payRate[i] = kb.nextFloat();行。

以下是更正后的代码。查看完整的工作代码here

public class ThreeArrays {

    public static void main(String args[]) {
        float[] payRate = new float[5];
        float[] hours = new float[5];
        String[] name = new String[5];

        getPayData(name, hours, payRate);

        displayGrossPay(name, hours, payRate);
    }

    public static void getPayData(String[] name, float[] hours, float[] payRate) {
        Scanner kb = new Scanner(System.in);

        for (int i = 0; i < hours.length; i++) {

            System.out.print("Enter the employee's name: ");
            name[i] = kb.nextLine();

            System.out.print("Enter the employee's hours: ");
            hours[i] = kb.nextFloat();

            System.out.print("Enter the employee's hourly rate: ");
            payRate[i] = kb.nextFloat();

            if(i < hours.length-1) kb.next();
        }

    }

    public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
        for (int i = 0; i < hours.length; i++) {
            System.out.println("Employee name: " + name[i] + " Gross Pay: " + (hours[i] *
                payRate[i]));
        }
    }
}

答案 2 :(得分:-1)

我已经把它想出来了

问题在于使用

 System.out.print("Enter the employee's name: ");
 name[i] = kb.nextLine();

我不得不将其改为

System.out.print("Enter the employee's name: ");
name[i] = kb.next();