Java (修改)如何显示与数组匹配的输入?

Java (修改)如何显示与数组匹配的输入?,java,Java,有人能告诉我这个代码的错误吗?我的问题是,如果输入在数组中,则显示“已找到”;如果输入不在数组中,则显示“未找到”。为什么我只能为我键入的内容显示“已找到” String [] deptName = {"Accounting", "Human Resources","Sales"}; //input String key = JOptionPane.showInputDialog("Enter a department: "); for(int i=0; i<deptName.leng

有人能告诉我这个代码的错误吗?我的问题是,如果输入在数组中,则显示“已找到”;如果输入不在数组中,则显示“未找到”。为什么我只能为我键入的内容显示“已找到”

String [] deptName = {"Accounting", "Human Resources","Sales"};

//input
String key = JOptionPane.showInputDialog("Enter a department: ");

for(int i=0; i<deptName.length; i++)
{
    if(deptName [i] == key);
}
System.out.println("Found");
String[]deptName={“会计”、“人力资源”、“销售”};
//输入
String key=JOptionPane.showInputDialog(“输入部门:”);
for(int i=0;i“found”总是打印出来的,因为它不在for循环中。而您的检查(也有问题)实际上什么都不做

试一试

“found”总是打印出来的,因为它不在for循环中。而您的支票(也有问题)实际上什么都不做

试一试


如果在java中的对象上使用==,则只检查对象是否完全相同。要比较对象的内容,应使用equals()

对于类字符串,equals()比较实际字符串内容

deptName[i].equals(key);  //returns true if text is the same

如果在java中的对象上使用==,则只检查对象是否完全相同。要比较对象的内容,应使用equals()

对于类字符串,equals()比较实际字符串内容

deptName[i].equals(key);  //returns true if text is the same

您有几个问题,首先需要调用
equals()
,并且不能用“;”终止if

for(int i=0; i<deptName.length; i++)  {         
    if(deptName [i].equals( key )) {
       System.out.println("Found"); 
    }
}

for(int i=0;i您有几个问题,首先需要调用
equals()
,并且不能用“;”终止if

for(int i=0; i<deptName.length; i++)  {         
    if(deptName [i].equals( key )) {
       System.out.println("Found"); 
    }
}
for(inti=0;i
inti=0;
对于(;i
inti=0;
对于(;我这样做

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i]){
       JOptionPane.showMessageDialog(null,"Found"); break;
}}
if(i==deptName.length)
   {JOptionPane.showMessageDialog(null,"Not Found");}
for(inti=0;i这样做

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i]){
       JOptionPane.showMessageDialog(null,"Found"); break;
}}
if(i==deptName.length)
   {JOptionPane.showMessageDialog(null,"Not Found");}
for(int i=0;i拧入环路

JOptionPane.showMessageDialog(null,Arrays.asList(deptName).contains(key) ? "Found" : "Not Found"); 
我知道这可能是一个编程练习,所以给你

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i])
       break;
}

if(i==deptName.length)
   JOptionPane.showMessageDialog(null,"Not Found");
else
   JOptionPane.showMessageDialog(null,"Found at " + i);
for(int i=0;i拧入环路

JOptionPane.showMessageDialog(null,Arrays.asList(deptName).contains(key) ? "Found" : "Not Found"); 
我知道这可能是一个编程练习,所以给你

for(int i=0;i<deptName.length;i++){
    if(key.equals(deptName[i])
       break;
}

if(i==deptName.length)
   JOptionPane.showMessageDialog(null,"Not Found");
else
   JOptionPane.showMessageDialog(null,"Found at " + i);

for(int i=0;iI已经这样修改了我的代码,我怎么能让它不显示3次呢?String[]deptName={“Accounting”,“Human Resources”,“Sales”};//input String key=JOptionPane.showInputDialog(“Enter a department:”);for(int i=0;iI已经这样修改了我的代码,我怎么能让它不显示3次呢?String[]deptName={“会计”、“人力资源”、“销售”};//输入字符串key=JOptionPane.showInputDialog(“输入部门:”);用于(int i=0;iHi,我编辑了您的问题,以便以后的任何人都可以看到整个对话,而不仅仅是最新的步骤。嗨,我编辑了您的问题,以便以后的任何人都可以看到整个对话,而不仅仅是最新的步骤。