2

如何打印输入与数组元素匹配的整行?请看看我的代码我哪里出错了...... :(

package scanner;
import java.util.Arrays;
import java.util.Scanner;

public class EmployeeInformation {

    static Scanner sc = new Scanner(System.in);

    static String info[][] = {{"09-001", "Ja Gb", "100", "10", },
                        {"09-002", "Justine", "200", "20", ""},
                        {"09-003", "Ja Ja", "150", "15", ""}};

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.print("     - MENU -\n");
        System.out.print("A. Search Record\nB. Payroll Summary\n------------------\nEnter choice: ");
        String choice = null;
        choice = sc.nextLine();


        if (choice.equalsIgnoreCase("a")) {
            System.out.print("Enter Employee #: ");
            String EmpNum = sc.nextLine();
            SearchRecord(EmpNum);
        }
        else {
                PayrollSummary();
            }
    }

    private static void SearchRecord(String employeeNumber) {
        // TODO Auto-generated method stub
        String row[] = new String[3];
        int i = 0;
        while(i <= info.length) {
            int j = 0;
            while(j <= info.length) {
                if(employeeNumber.equals(info[i][j])) {
                    for (int a = 0; a <= row.length; a++) {
                        row[a] = info[i][j];
                        System.out.print(row[a]);
                        a++;
                    }
                }
                else {
                    System.out.print("Invalid employee number.");
                    System.exit(0);
                }
                j++;
            }
            i++;
        }
    }

    private static void PayrollSummary() {

        System.out.println("Employee #:\tEmployee Name\tRate per Hour\tTotal Hours Worked\tGross Pay");
        int r = 0;
        while ( r <= info.length - 1) {
            int c = 0;
            while ( c <= info.length ) {
                System.out.print(info[r][c] + "\t\t");
                if (c == 3) {
                System.out.print("\n");
                }
                c++;
            }
            r++;
        }



        //for (int a = 0; a <= info.length -1; a++) {
        //      Integer.parseInt(info[a][4]) = Integer.parseInt((info[a][3]) *  (info[a][4]));

        //}
    }
}

当输入匹配一个元素时我应该怎么做?请给我一个提示

4

2 回答 2

1

看起来您始终将员工编号作为二维数组中的第一个元素。在这种情况下,您根本不需要嵌套循环:

String[] matchedRow;
for(int i=0; i<info.length; i++)
{
    String[] oneRow = info[i];
    if(oneRow[0].equals(employeeNumber))
    {
        matchedRow = oneRow;
        break
    }
}

for(int i=0; i<matchedRow.length; i++)
{
    System.out.println(matchedRow[i]);
}

也就是说,在现实生活中,您很少会像您那样将员工记录放在数组中。您将创建一个Employee类并拥有一个List对象Employee。如果Employee类的设计正确且正确实现了equals,hashCodetoString,则 match 方法将非常简单:

employee = employeeList.get(employeeList.indexOf(employee));
System.out.println(employee);
//Do whatever with employee
于 2012-10-09T18:26:22.987 回答
0

尝试这个 :

class Employee {

     static String info[][] = {{"09-001", "Ja Gb", "100", "10", },
            {"09-002", "Justine", "200", "20", ""},
            {"09-003", "Ja Ja", "150", "15", ""}};


     private static void search(String employeeNumber) {


        for (String s[] : info ) {
            if(employeeNumber.equals(s[0])) {
                for (String row : s) {
                    System.out.println (row) ; 
                }
                return ;
            }       
        }

        System.out.println ("Nothing found") ; 

    }

    public static void main(String args[]) {
        search ("09-003");
    }//end of main


}
于 2012-10-09T18:44:40.790 回答