java - 如何将 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 秒)
解决方案
您将 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])
它将检查对话框中的输入与数组。
推荐阅读
- git - Git merge --abort:错误:未跟踪的工作树文件'src/components/footer.jsx'将被合并覆盖
- python - 比较两个 CSV 文件并打印差异
- microsoft-graph-toolkit - Angular/JS 方法为“loginCompleted”添加EventListener?
- python - Networkx 网络边缘
- python - lxml 是否支持自定义非 XML、非 HTML 解析器的使用?
- php - markdown:将块元素内的换行符渲染为
- oauth-2.0 - 拨打客服导致401 Unauthorized
- entity-framework - linq 查询中的条件位置
- statistics - 从一个直方图,从平均值或中位数创建一个新的直方图?
- java - 如何将类对象称为方法参数