2016-07-26 116 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