gpt4 book ai didi

java - 如何打印两个不同数据类型的数组?

转载 作者:行者123 更新时间:2023-12-01 16:15:32 24 4
gpt4 key购买 nike

有人可以帮忙吗?选择 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)静态无效搜索(字符串[] 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:是的,扫描仪有一个问题,它要么无法读取整行,要么您需要在获取输入之前刷新流。它在一堆简单的程序中给我带来了很多问题。然后我转而使用 InputStreamReader 和 BufferedStreamReader 组合。只需将它们包装在 try-catch block 中,就可以了。研究一下它,它会让你的代码行为和你的生活变得更加轻松。

7:我希望这对您有所帮助。

关于java - 如何打印两个不同数据类型的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com