簡體   English   中英

如何將 JOptionPane.showInputDialog 與數組一起使用?

[英]How do I use JOptionPane.showInputDialog with an array?

我試過只使用字符串而不是數組中的數字,但它仍然不起作用。

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”時會顯示一條消息,如果他們選擇其他任何內容,我希望顯示一個不同的消息。

run: Exception in thread "main" java.lang.NullPointerException at practice.pkg3.Practice3.main(Practice3.java:28) /Users/alexanderkiknadze/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53 : Java 返回: 1 BUILD FAILED (總時間: 12 秒)

您將 String[] 傳遞給您的 JOptionPane.showInputDialog。 因此,根據選擇,您將獲得一個字符串而不是字符串 []。 所以你應該改變你的代碼

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]) ,這樣它就會檢查對話框中的輸入和數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM