2016-07-26 76 views
-1

我寫了這個代碼爲我的更新類展示,但由於某種原因,它不會得到在Joptionpane}else{語句後在底部:Java的JOptionPane的不else語句

JOptionPane.showMessageDialog(null,"Currently no updates available!!");

相反,它會公正return出..這是好的,但我也想它通知用戶。我與格式更新中... intcurrentversion = 2,所以它應該運行Joptionpane

public static void main(String[] args) throws IOException { 
    ImageIcon updateicon = new ImageIcon("resources/update.png"); 
    String url = "http://example.com/wp-content/uploads/version/Version.html"; 
    Document document = Jsoup.connect(url).get(); 
    String getcurrentversion = document.select("version").first().text(); 
    int intcurrentversion = Integer.parseInt(getcurrentversion);  
    System.out.println(intcurrentversion); 

    if (intcurrentversion > 2) { 

     int response = JOptionPane.showConfirmDialog(null, "Would you like to update?", "Update available!", 
      JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,updateicon); 

     if (response == JOptionPane.NO_OPTION) { 
      System.out.println("No button clicked"); 
     } else if (response == JOptionPane.YES_OPTION) { 
      System.out.println("Yes button clicked"); 

      { 
       String filePath = "C:/Windows/Updater.exe";// 
       try { 
        Process p = Runtime.getRuntime().exec(filePath); 

       } catch (Exception g) { 
        g.printStackTrace(); 
       } 
      } 


     } else { 
      JOptionPane.showMessageDialog(null,"Currently no updates available!!"); 

     } 
    } 
} 

}

+1

如果您修復縮進,所以縮進ma看看代碼實際上做了什麼,那麼它將是顯而易見的。 – immibis

+0

這就是爲什麼代碼格式化非常重要。不要忽視它。 –

+0

是的,抱歉,希望這是更好的! – javajoejuan

回答

2

這裏是固定的縮進你的程序:

import java.io.IOException; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

public class CheckForUpdate { 

    public static void main(String[] args) throws IOException { 
     ImageIcon updateicon = new ImageIcon("resources/update.png"); 
     String url = "http://example.com/wp-content/uploads/version/Version.html"; 
     Document document = Jsoup.connect(url).get(); 
     String getcurrentversion = document.select("version").first().text(); 
     int intcurrentversion = Integer.parseInt(getcurrentversion);  
     System.out.println(intcurrentversion); 

     if (intcurrentversion > 0) { 

      int response = JOptionPane.showConfirmDialog(null, "Would you like to update?", "Update available!", 
       JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,updateicon); 

      if (response == JOptionPane.NO_OPTION) { 
       System.out.println("No button clicked"); 
      } else if (response == JOptionPane.YES_OPTION) { 
       System.out.println("Yes button clicked"); 

       { 
        String filePath = "C:/Windows/Updater.exe";// 
        try { 
         Process p = Runtime.getRuntime().exec(filePath); 

        } catch (Exception g) { 
         g.printStackTrace(); 
        } 
       } 


      } else { 
       JOptionPane.showMessageDialog(null,"Currently no updates available!!"); 

      } 
     } 
    } 
} 

我希望這是現在很明顯,爲什麼消息不會顯示當intcurrentversion < = 0.

+0

謝謝你,我改變了一下我的問題。所以'intcurrentversion = 2',我把它當作'if(intcurrentversion> 2){'所以它應該運行'JOptionPane'。 – javajoejuan

+1

@javajoejuan:他已經解釋了爲什麼以前和當前版本不能按照您的預期工作。使用良好的縮進,然後用你的眼球告訴你其他什麼級別。 1+這個答案。 –

+0

謝謝你的回答! – javajoejuan