我嘗試過僅使用字符串而不是數組中的數字,但它仍然不起作用。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"); }我希望這樣,當他們選擇“不”時,會顯示一條消息,如果他們選擇其他任何內容,我希望顯示另一條消息。運行:在practice.pkg3.Practice3.main(Practice3.java:28) /Users/alexanderkiknadze/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53 線程“main”java.lang.NullPointerException 中出現異常: Java 返回: 1 BUILD FAILED (總時間: 12 秒)
1 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
您將 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])
添加回答
舉報
0/150
提交
取消