java - 如何打印两个不同的数据类型数组?
问题描述
任何人都可以帮忙吗?选择 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 也不适合我。
在完全阅读您的代码之前,我的直觉是失败的原因是使用 Scanner 类来获取员工的姓名。我过去也遇到过类似的问题,最好的办法是学习使用 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() 函数做任何事情
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:是的,Scanner 有一个问题,它要么不读取整行,要么您需要在获取输入之前刷新流。它在一堆简单的程序中给我带来了很多问题。然后我切换到使用 InputStreamReader 和 BufferedStreamReader 组合。只需将它们包装在 try-catch 块中,就可以了。研究一下,它会让你的代码行为和你的生活变得更轻松。
7:我希望这会有所帮助。
推荐阅读
- php - 在数据表列上设置 Ajax 响应
- python - 当我尝试运行此脚本时,我收到此错误“django.core.exceptions.FieldDoesNotExist: No_Address has no field named 'False'”
- git - Git在写作期间冻结
- react-native - 在 React Native 中打开键盘时如何使 TextInput 居中
- jquery - 如何在 jquery 中更改类名
- java - 在具有最小索引的数组中找到第一个重复元素
- javascript - jQuery 数字计数器不接受逗号
- sql - ORA-00936 ROW NUMBER 上缺少表达式
- sockets - 套接字写入小端
- ruby-on-rails - Rails simple_form:在提交时传递参数