繁体   English   中英

如何打印两种不同的数据类型 arrays?

[英]How to print two different data type arrays?

任何人都可以帮忙吗? 选择 2 不起作用。 假设在用户输入员工姓名时显示员工 ID,但当用户输入姓名时,什么都不会打印。 代码没有错误。

public static void main(String[] args) {
int[] emplID={ 42577, 38611, 32051, 28627, 42061, 79451 };//employee ID
int ID = employeeID(emplID);
String[] emplNames= { "Bruce Wayne", "Barry Allen", "Hal Jordan", "Dinah Lance", "Oliver Queen", "Tineil Charles" };// Employee Names
search(emplNames, emplID);
//methods called from main


}

public static int employeeID(int [] emplID) {
    //check ID length
    for(int i=0; i< emplID.length; i++) {
        if((emplID[i] > 10000)&&(emplID[i] < 99999)) {
        System.out.print(emplID[i] + " - Valid ID length\n");

        }
        else {
            System.out.println(emplID[i] + " - Invalid ID! ID must be Five digits!\n");

        }//end of check length

        //check if ID is prime
         boolean isPrime = true;
            for (int j = 2; j < emplID[i]; j++) {
                if (emplID[i] % j == 0) {
                    System.out.println(emplID[i] + " - not prime");
                    isPrime = false;
                    break;
                } 
            }
            if(isPrime) System.out.println(emplID[i] + " - valid prime");//end of check prime
        }//end of employeeID method
    return 0;

}// end of ID checker

// 搜索员工数据 public static void search(String[] emplNames, int[]emplID) {

        Scanner scan= new Scanner(System.in);
        //Menu Choice
        System.out.println("Please choose 1 to enter Employee ID or 2 to enter Employee Name:" );           

        int num = scan.nextInt();//input choice

        // Choice 1 to enter ID to display name
            if (num == 1) {
            System.out.println("Please enter Employee ID:");
            int searchID= scan.nextInt();
            for(int ID = 0; ID < emplID.length; ID++) {
            if (searchID == (emplID[ID])){
            System.out.println("Name: "+ emplNames[ID]);
        }
    }
            }
        // Choice 2 to enter name to display ID
            else if(num == 2) {
            System.out.println("Please enter Employee Name");
            String searchName= scan.next();
            for(int ID = 0; ID< emplID.length; ID++){
            if ((searchName.equals(emplNames[ID]))){
            System.out.println("ID: " + emplID[ID]);
            }
            }
            }   
            else 
                System.out.println("Employee Not Found");
}
}

我复制并粘贴了您的代码并在我的机器上运行它。 是的,选择 2 也不适合我。

在完全阅读您的代码之前,我的直觉是失败的原因是使用扫描仪 class 来获取员工的姓名。 我过去也遇到过类似的问题,最好的办法是学习使用 InputStreamReader 和 BufferedStreamReader 对象。

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

1:我没有对你的 main() 做任何事情

public static void main(String[] args) {
    int[] emplID={ 42577, 38611, 32051, 28627, 42061, 79451 };//employee ID
    int ID = employeeID(emplID);
    String[] emplNames= { "Bruce Wayne", "Barry Allen", "Hal Jordan", "Dinah Lance", "Oliver Queen", "Tineil Charles" };// Employee Names
    search(emplNames, emplID);
}

2:我没有对你的employeeID() function 做任何事情

public static int employeeID(int [] emplID) {
    //check ID length
    for(int i=0; i< emplID.length; i++) {
        if((emplID[i] > 10000)&&(emplID[i] < 99999)) {
            System.out.print(emplID[i] + " - Valid ID length\n");

        }
        else {
            System.out.println(emplID[i] + " - Invalid ID! ID must be Five digits!\n");

        }//end of check length

        //check if ID is prime
        boolean isPrime = true;
        for (int j = 2; j < emplID[i]; j++) {
            if (emplID[i] % j == 0) {
                System.out.println(emplID[i] + " - not prime");
                isPrime = false;
                break;
            }
        }
        if(isPrime) System.out.println(emplID[i] + " - valid prime");//end of check prime
    }//end of employeeID method
    return 0;

}// end of ID checker

3:在您的 search() 方法中,我首先创建了 InputStreamReader 和 BufferedReader:

public static void search(String[] emplNames, int[]emplID) {
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader buff = new BufferedReader(in);

    //Menu Choice
    System.out.println("Please choose 1 to enter Employee ID or 2 to enter Employee Name:" );
    int num = 0;
    try {
        num = Integer.parseInt(buff.readLine());
    } catch (Exception e) {
        e.printStackTrace();
    }

4:由于选项 1 工作正常,我所做的只是将您的 for 循环更改为 for-each 循环以使其更易于阅读。

    // Choice 1 to enter ID to display name
    if (num == 1) {
        System.out.println("Please enter Employee ID:");
        int searchID = 0;
        try {
            searchID = buff.read();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i : emplID) {
            if (searchID == i) {
                System.out.println("Name: " + emplNames[i]);
            }
        }

5:这是我为使您的第二个选项起作用所做的工作。 同样,通过 BufferedReader 对象的 readLine() 方法从用户那里获取字符串。 然后,它只是让您的 for 循环搜索匹配项。 而已。 之后,我运行该程序并针对您上面的所有名称进行了测试,工作正常。

    } else if (num == 2) {
        System.out.println("Please enter Employee Name");
        String searchName = "";
        try {
            searchName = buff.readLine();
        } catch(Exception e) {
            e.printStackTrace();
        }

        for(int ID = 0; ID< emplID.length; ID++){
            if ((searchName.equals(emplNames[ID]))){
                System.out.println("ID: " + emplID[ID]);
            }
        }
    } else {
        System.out.println("Employee Not Found");
    }
  }
}

6:是的,扫描仪有一个问题,它要么无法读取整行,要么您需要在获取输入之前刷新 stream。 它在一堆简单的程序中给我带来了很多问题。 然后我切换到使用 InputStreamReader 和 BufferedStreamReader 组合。 只需将它们包装在 try-catch 块中,就可以了。 研究一下,它会让你的代码行为和你的生活更轻松。

7:我希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM