W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
有時(shí)需要有多個(gè)if語(yǔ)句嵌入在彼此內(nèi)部。
這種說(shuō)法的一般形式是 -
if(condition) { statement #1 statement #2 ... } else if(condition) { statement #3 statement #4 } else { statement #5 statement #6 }
以下是嵌套if / else語(yǔ)句的示例 -
class Example { static void main(String[] args) { // Initializing a local variable int a = 12 //Check for the boolean condition if (a>100) { //If the condition is true print the following statement println("The value is less than 100"); } else // Check if the value of a is greater than 5 if (a>5) { //If the condition is true print the following statement println("The value is greater than 5 and greater than 100"); } else { //If the condition is false print the following statement println("The value of a is less than 5"); } } }
在上面的例子中,我們首先將一個(gè)變量初始化為值12.在第一個(gè) if 語(yǔ)句中,我們看到 a 的值是否大于100。如果沒(méi)有,那么我們進(jìn)入第二個(gè)for循環(huán),看看 a 的值是否大于5或小于5.上面的代碼的輸出將是 -
The value is greater than 5 and greater than 100
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: