首页 > 解决方案 > 如何将 JOptionPane.showInputDialog 与数组一起使用?

问题描述

我试过只使用字符串而不是数组中的数字,但它仍然不起作用。

String [] seller  = { "Yeah", "Nah" ,"Depends on what it is"};
      seller = (String[]) JOptionPane.showInputDialog(null, "Hey, you want to see something cool","Scam3r69 has entered chat", 1, null, seller, seller[1]); 

if (seller.equals(seller[2])){
   JOptionPane.showMessageDialog(null, "Just let me say what it is okay");
}
else{
   JOptionPane.showMessageDialog(null,"It's a chance to make 1 million dollars in 1 minute");  
}

我希望这样当他们选择“Nah”时会显示一条消息,如果他们选择其他任何内容,我希望显示一个不同的消息。

运行:在 practice.pkg3.Practice3.main(Practice3.java:28) /Users/alexanderkiknadze/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53 线程“主”java.lang.NullPointerException 中的异常:Java 返回:1 BUILD FAILED(总时间:12 秒)

标签: java

解决方案


您将 String[] 传递给您的 JOptionPane.showInputDialog。因此,根据您的选择,您将获得一个 String 而不是 String [] 。所以你应该改变你的代码

seller = (String[]) JOptionPane.showInputDialog(null, "Hey, you want to see something cool","Scam3r69 has entered chat", 1, null, seller, seller[1]); 


String sellerInput = (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])它将检查对话框中的输入与数组。


推荐阅读