忽略该用户需要输入String

时间:2014-06-19 00:30:32

标签: java

我是编程方面的新手,我是第一堂课,下周我们有一个项目。现在,我知道我的代码中有很多东西可以修复,但我想弄清楚为什么它不会让我输入ssn。 这是我到目前为止运行它的程序:


本月:10

当前周:2

员工人数:3

员工资料#1

薪酬类型(每小时(h),薪水,薪水加佣金(c)):h

姓名:Biff Sanchez

社会安全号码:生日月(1-12):


在我要求输入生日月之前,我无法输入社会安全号码。我尝试过nextLine而不是next,但它没有改变任何东西。

我的代码如下,请帮忙。谢谢你!


package payroll;

import java.util.Scanner;
import java.text.*;

public class PayrollProgram {

    public static void main(String [] args) {

        DecimalFormat df = new DecimalFormat("###.00");
        Scanner kybd = new Scanner(System.in);

        //get current month
        System.out.print("Current Month: ");
        int currentMonth = kybd.nextInt();
        if (currentMonth < 1 || currentMonth > 12)
        {
            System.out.print("error");
            System.exit(0);
        }

        //get current week
        System.out.print("Current Week: ");
        int currentWeek = kybd.nextInt();
        if (currentWeek < 1 || currentWeek > 4)
        {
            System.out.print("error");
            System.exit(0);
        }

        //get number of employees
        System.out.print("Number of Employees: ");
        int numEmployee = kybd.nextInt();
        int[] list = new int[numEmployee];

        //declare variables needed for paycheck report
        //String type1 = "";
        String [] type = new String[list.length];
        String [] name = new String[list.length];
        String [] ssn = new String[list.length];
        double [] pay = new double[list.length];
        double money = 0;

        //get information for each employee
        for(int i = 0; i < list.length; i++)
        {
            System.out.print("PROFILE FOR EMPLOYEE # " + (i + 1) + "\n");

            //get type of pay
            System.out.print("Type of Pay (Hourly (h), Salaried (s), Salaried plus Commision (c)): ");
            type[i] = kybd.next();
            if (!type[i].equals("h") && !type[i].equals("s") && !type[i].equals("c")) 
            {
                System.out.print("error");
                System.exit(0);
            }

            //get name
            System.out.print("Name: ");
            name[i] = kybd.next();

            //get ssn
            System.out.print("Social Security Number: ");
            ssn[i] = kybd.next();

            //get birthday month
            System.out.print("Birthday Month (1-12): ");
            int bdayMonth = kybd.nextInt();
            if (bdayMonth < 1 || bdayMonth > 12)
            {
                System.out.print("error");
                System.exit(0);
            }

            //get birthday bonus week
            System.out.print("Birthday Bonus Week (1-4): ");
            int bdayWeek = kybd.nextInt();
            if (bdayWeek < 1 || bdayWeek > 4)
            {
                System.out.print("error");
                System.exit(0);
            }

            //get hourly pay (if hourly)
            if (type.equals("h"))
            {
                System.out.print("Hourly Pay:");
                double hourlyPay = kybd.nextDouble();
                System.out.print("Hours worked this past week:");
                double hours = kybd.nextDouble();
                money = hourlyPay * hours;
            }

            //get salary if salaried
            else if (type.equals("s"))
            {
                System.out.print("Salary:");
                double salary = kybd.nextDouble();
                money = salary;
            }

            //get salary if salaried plus commission
            else if (type.equals("c"))
            {
                System.out.print("Salary:");
                double salary = kybd.nextDouble();
                money = salary;

                System.out.print("Sales for this past week: ");
                double salesWeek = kybd.nextDouble();

                System.out.print("Sales Commission Rate (fraction paid to employee): ");
                double commissionRate = kybd.nextDouble();

                money = (commissionRate * salesWeek) + money;

            }

            if (bdayMonth == currentMonth && bdayWeek == currentWeek)
                money = money + 100;

            System.out.print("\n");
            pay[i] = money;

            kybd.close();
        }
        System.out.print("\n");
        /*
         */

        // shows final results
        System.out.print("PAYCHECK REPORT:\n");
        for(int i = 0; i < list.length; i++)
        {
        System.out.print("\nEmployee:" + name[i]);
        System.out.print("\nSocial Secuirty Number:" + ssn[i]);
        System.out.print("\nPaycheck:" + df.format(pay[i]));
        System.out.print("");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题出在名称的输入而不是SSN中。

更改此

    name[i] = kybd.next();

    name[i] = kybd.nextLine();

由于输入名称可以包含空格,因此您需要在此处使用nextLine。

答案 1 :(得分:1)

我在没有使用nextLine()的情况下找到了解决方案,由于某些原因,我遇到了麻烦。

    //get name
    System.out.print("Name: ");
    String nameF = kybd.next();
    String nameL = kybd.next();
    name[i] = nameF + nameL