试图找出我的代码有什么问题 - 我希望使用浓度数组检查用户输入,如果他们输入任何数组以打印出积极的显示消息......但是,我得到“是的,无论输入如何,每次都是有效的浓度。
import javax.swing.JOptionPane;
public class BASIT_Concentration_Check
{
public static void main(String[] args)
{
String[] concentrations = {"DTP","HCIT","INFS","NTEL","WDM"};
String studentconcentration = getStudentConcentration();
boolean concentrationvalid = isConcentrationValid(studentconcentration, concentrations);
displayMessage(concentrationvalid);
}
public static String getStudentConcentration()
{
String studentconcentration = JOptionPane.showInputDialog(null,"What is your B.S. AIT Concentration?");
return studentconcentration;
}
public static boolean isConcentrationValid(String studentconcentration, String [] concentrations)
{
boolean concentrationvalid=false;
for (int i=0;i<concentrations.length;i++)
{
if (concentrations[i]==studentconcentration)
{
concentrationvalid = true;
}
}
return concentrationvalid;
}
public static void displayMessage(boolean concentrationvalid)
{
if (concentrationvalid==true)
{
JOptionPane.showMessageDialog(null, "Yes, that is a valid concentration");
}
else
{
JOptionPane.showMessageDialog(null,"I'm sorry, that is not a valid concentration");
}
}
}