Java 如何将JOptionPane.showInputDialog与数组一起使用?
我试着只使用字符串而不是数组中的数字,但仍然不起作用Java 如何将JOptionPane.showInputDialog与数组一起使用?,java,Java,我试着只使用字符串而不是数组中的数字,但仍然不起作用 String[]seller={“是”、“不”、“取决于它是什么”}; seller=(String[])JOptionPane.showInputDialog(null,“嘿,你想看一些酷的东西”,“Scam3r69已经进入聊天”,1,null,seller,seller[1]); 如果(卖方等于(卖方[2])){ showMessageDialog(null,“让我说什么好”); } 否则{ showMessageDialog(null,
String[]seller={“是”、“不”、“取决于它是什么”};
seller=(String[])JOptionPane.showInputDialog(null,“嘿,你想看一些酷的东西”,“Scam3r69已经进入聊天”,1,null,seller,seller[1]);
如果(卖方等于(卖方[2])){
showMessageDialog(null,“让我说什么好”);
}
否则{
showMessageDialog(null,“这是一个在一分钟内赚一百万美元的机会”);
}
我希望这样,当他们选择“不”时,会显示一条消息,如果他们选择了其他内容,我希望显示不同的内容
运行:线程“main”java.lang.NullPointerException中的异常位于
practice.pkg3.Practice3.main(Practice3.java:28)
/Users/alexanderkiknadze/Library/Caches/NetBeans/8.2/executor snippets/run.xml:53:
Java返回:1次生成失败(总时间:12秒)
您正在将字符串[]传递给JOptionPane.showInputDialog。因此,根据选择,您将得到一个字符串,而不是字符串[]。所以你应该改变你的代码
seller = (String[]) JOptionPane.showInputDialog(null, "Hey, you want to see something cool","Scam3r69 has entered chat", 1, null, seller, seller[1]);
到
和
seller.equals(seller[2])
到sellerInput.equals(seller[2])
,这样它就可以用数组检查对话框中的输入。另外,我对java非常陌生,如果我遗漏了任何明显的内容,我很抱歉。谢谢,我真的很感激,我真的被困在这个问题上了。请仔细想想,接受这个答案,因为它可能会在将来帮助别人-
String sellerInput = (String) JOptionPane.showInputDialog(null, "Hey, you want to see something cool","Scam3r69 has entered chat", 1, null, seller, seller[1]);