-2

一只忙碌的猫 http://maradastudios.ucoz.com/school/Capture3.png

出于某种原因,在输入员工姓名时,插入点会跳过员工 1 并直接转到员工 2。但是,员工 3 及以后的员工似乎会正确输入。这是我的完整代码。忽略它的愚蠢,我做事非常规.import

java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.*;
import java.text.*;
public class program1
{
    public static boolean isclearing=false;
    public static int[] num={0,0,0,0,0,0,0};
    public static String[] name={"","","","","","",""};
    public static String[] dept={"","","","","","",""};
    public static double[] rate={0,0,0,0,0,0,0};
    public static int[] hours={0,0,0,0,0,0,0};
    public static int[] code={0,0,0,0,0,0,0};
    public static Scanner in=new Scanner(System.in);
    public static NumberFormat money=NumberFormat.getCurrencyInstance();
    public static int inval=0;
    public static void main(String[] args)
    {
        while(inval!=7)
        {
            showmenu();
            inval=in.nextInt();
            switch(inval)
            {
                case 1:
                {
                    if(isclearing==false){
                    input();
                    break;}
                }
                case 2:
                {
                    employees();
                    break;
                }
                case 3:
                {
                    production();
                    break;
                }
                case 4:
                {
                    machineshop();
                    break;
                }
            }
        }
    }
    public static void showmenu()
    {
        System.out.println("\n1. Enter data");
        System.out.println("2. Show all employees");
        System.out.println("3. Show Production employees");
        System.out.println("4. Show Machine Shop employees");
        System.out.println("5. Gross pay report");
        System.out.println("6. Display by union (618/410)");
        System.out.print("7. Exit\nEnter your choice >> ");
        inval=in.nextInt();
        clearme();
        for(int crap=0;crap<99999;crap++){}
    }
    public static void input()
    {
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee number "+(i+1)+": ");
            num[i]=in.nextInt();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee name "+(i+1)+": ");
            name[i]=in.nextLine();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee department "+(i+1)+": ");
            dept[i]=in.nextLine();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter current rate "+(i+1)+": ");
            rate[i]=in.nextDouble();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter current hours "+(i+1)+": ");
            hours[i]=in.nextInt();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter union code "+(i+1)+": ");
            code[i]=in.nextInt();
        }
    }
    public static void employees()
    {
        System.out.println("Employee\tEmployee\tEmployee");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
            System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
        }
    }
    public static void production()
    {
        System.out.println("Employee\tEmployee\tEmployee  ");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            if(dept[i].equals("Production"))
            {
                //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
                System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
            }
        }
    }
    public static void machineshop()
    {
        System.out.println("Employee\tEmployee\tEmployee  ");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            if(dept[i].equals("Machine Shop"))
            {
                //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
                System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
            }
        }
    }
    public static void clearme()
    {
        isclearing=true;
        try
        {
            Robot pressbot = new Robot();
            pressbot.keyPress(17); // Holds CTRL key.
            pressbot.keyPress(76); // Holds L key.
            pressbot.keyRelease(17); // Releases CTRL key.
            pressbot.keyRelease(76); // Releases L key.
            pressbot.keyPress(KeyEvent.VK_SPACE);
            pressbot.keyRelease(KeyEvent.VK_SPACE);
            pressbot.keyPress(KeyEvent.VK_ENTER);
            pressbot.keyRelease(KeyEvent.VK_ENTER);
        }
        catch (AWTException ex)
        {
            //Hi.
        }
        isclearing=false;
    }
}
4

1 回答 1

2

Scanner#nextInt() does not consume newline characters, instead passing through to the loop that reads employee names.

name[i] = in.nextLine();

The first line in the iteration now will not block as it has already received input from the previous block.

Solution: add in.nextLine() prior to the second loop to consume the newline character.

于 2013-04-22T16:56:40.987 回答