2016-12-04 152 views
-7
int num=6; 
num=num+1; 
if (num>6) 
jTextField1.setText(Integer.toString(num)); 
else 
jTextField1.setText(Integer.toString(num+5)); 

我很困惑與將在jTextField1顯示,如果它應該是12或11輸出。如果聲明JAVA

我還有一個問題, 在下面的例子中,我們將使用「num = 7」或「num == 7」?

int num=6; 
if (num==7) 
jTextField1.setText("a"); 
else 
jTextField1.setText("b"); 
+1

對於第一個問題 - 您可以嘗試運行代碼以查看它的功能。 –

+1

「我對輸出感到困惑」 - 當你運行代碼時你會得到什麼? – jay

+0

請幫我解決第二個問題,我很困惑。 – Alandvnso

回答

0

您參考下面的代碼註釋:

int num=6;//num is 6 here 
num=num+1;//num is 7 after this step 
if (num>6)//num is greater than 6 ? YES, so below line will be executed 
jTextField1.setText(Integer.toString(num));//It comes here & sets jTextField1 as 7 
else 
jTextField1.setText(Integer.toString(num+5)); 

在第一種情況下,如上所述,jTextField1會我們會使用什麼設置爲7

「NUM = 7」或「NUM == 7」?

num=7是分配7 num可變

num==7當你正在檢查的條件爲num則採用的是7只返回truefalse(需要注意的是,==是Java平等的運營商這是用來檢查兩個值是否相等)

我想知道如果我們使用if(num == 7)或if(num = 7)?

你永遠的ifwhilefor

健康檢查過程中使用num=7我建議你閱讀here和先了解的基本知識。

+0

當你說「不」時,你的意思是說? – TZHX

+0

好點,我的意思是YES – developer

+0

謝謝你的回答,但是我想知道如果我們使用if(num == 7)還是if(num = 7)? – Alandvnso

0

我添加了一些意見,你的代碼解釋語句的含義:

int num=6; // the variable named "num" is declared an int and assigned the value 6 
num=num+1; // the int "num" is assigned the value 7 
if (num>6) // 7 > 6 (true_ 
jTextField1.setText(Integer.toString(num)); // 7 displayed by jTextField1 
else 
jTextField1.setText(Integer.toString(num+5)); // not evaluated since 7 > 6 

而對於第二個問題:

num = 7意味着價值7被分配到變量numnum == 7' returns a true/false depending on the value of num`。