跳过Print语句

时间:2014-12-10 14:55:16

标签: java java.util.scanner system.out

我正在为类Project构建一些东西,代码仍然很混乱,请忽略它。 我问的问题是如何解决这个错误:

===================================

员工姓名| 尚史

工作小时数| 40

每小时费率| 9.75

员工姓名| //注意这里跳过输入问题"员工姓名"

工作小时数|

===================================

///代码:-----

import java.util.Scanner;

public class PayRollProject {
     public static void main(String[] args) {

        final double FEDERALTAX = .18;
        final double STATETAX = .045;
        final double HOSPITALIZATION = 25.65;
        final double UNIONDUES = 7.85;

        // Init. Variable
        Scanner Board = new Scanner(System.in);

        String[] name = new String[3];
        int[] hourWages = new int[3];
        double[] hourRate = new double[3];
        double[] grossPay = new double[3];
        double[] fedTax = new double[3];
        double[] stateTax = new double[3];
        double[] deductions = new double[3];
        double[] netPay = new double[3];



        //GP = HW * HR;
        //FW = GP * .18;
        int i, j, k;

        // Back Door

        for(k = 0; k < 3; k++) {
            System.out.println();

            System.out.println("Employee Name |");

            name[k] = Board.nextLine();

            System.out.println("Hours Worked |");

            hourWages[k] = Board.nextInt();

            System.out.println("Hourly Rate |");

            hourRate[k] = Board.nextDouble();

            System.out.println();
            System.out.println();
        }

        // input/ calculations

        for(j = 0; j < 3; j++) {
    /*      System.out.println();
            System.out.println("Employee Name |");
            name[j] = Board.nextLine();
            System.out.println("Hours Worked |");
            hourWages[j] = Board.nextInt();
            System.out.println("Hourly Rate |");
            hourRate[j] = Board.nextDouble();      */

            grossPay[j] = hourWages[j] * hourRate[j];
            fedTax[j] = grossPay[j] * .18;
            stateTax[j] = grossPay[j] * .045;
            netPay[j] = grossPay[j] - (fedTax[j] + stateTax[j] + HOSPITALIZATION + UNIONDUES);

            for(i = 0; i < 3; i++) {
                System.out.println("Employee    |           " + name[i]);
                System.out.println("Hours Work  |           " + hourWages[i]);
                System.out.println("Hourly Rate |           " + hourRate[i]);
                System.out.println("Gross Pay   |           " + grossPay[i]);
                System.out.println("");  //- < Blank!
                System.out.println("Deductions:                    ");
                System.out.println("Federal Withholding |          " + fedTax[i]);
                System.out.println("State WithHolding |            " + stateTax[i]);
                System.out.println("Hospitalization |              " + HOSPITALIZATION);
                System.out.println("Union Dues |                   " + UNIONDUES);
                System.out.println("                               -----");
                System.out.println("Total Deductions |             " + deductions[i]);
                System.out.println("                               ");
                System.out.println("NetPay |                       " + netPay[i]);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的for循环遇到问题,特别是您的j循环和i循环。 i循环位于j循环内部,看起来好像不应该循环。你应该

for(j = 0; j < 3; j++) {
/*  System.out.println();
    System.out.println("Employee Name |");
    name[j] = Board.nextLine();
    System.out.println("Hours Worked |");
    hourWages[j] = Board.nextInt();
    System.out.println("Hourly Rate |");
    hourRate[j] = Board.nextDouble();      */

    grossPay[j] = hourWages[j] * hourRate[j];
    fedTax[j] = grossPay[j] * .18;
    stateTax[j] = grossPay[j] * .045;
    netPay[j] = grossPay[j] - (fedTax[j] + stateTax[j] + HOSPITALIZATION + UNIONDUES);
}

// the loop above is complete; now we start the next one

for(i = 0; i < 3; i++) {
    System.out.println("Employee    |           " + name[i]);
    System.out.println("Hours Work  |           " + hourWages[i]);
    System.out.println("Hourly Rate |           " + hourRate[i]);
    System.out.println("Gross Pay   |           " + grossPay[i]);
    System.out.println("");  //- < Blank!
    System.out.println("Deductions:                    ");
    System.out.println("Federal Withholding |          " + fedTax[i]);
    System.out.println("State WithHolding |            " + stateTax[i]);
    System.out.println("Hospitalization |              " + HOSPITALIZATION);
    System.out.println("Union Dues |                   " + UNIONDUES);
    System.out.println("                               -----");
    System.out.println("Total Deductions |             " + deductions[i]);
    System.out.println("                               ");
    System.out.println("NetPay |                       " + netPay[i]);
}

这导致您遇到麻烦的原因是,当您仅计算第一个员工的结果时,您将为第二个和第三个员工打印输出。

答案 1 :(得分:0)

我想这个问题是由于使用了nextLine。在实际读取输入之前使用虚拟nextLine。这将在nextxxx之后读取并刷新输入,然后读取新的。或者您可以尝试使用next代替。

我希望有所帮助。还存在同样的问题here