{"id":704,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java If-else\u8bed\u53e5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/704.html","title":{"rendered":"Java If-else\u8bed\u53e5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java If-else\u8bed\u53e5<\/h1>\n<\/p><\/div>\n
\n Java
\n if\u8bed\u53e5<\/em>\u5b83\u68c0\u67e5\u5e03\u5c14\u503c\u6761\u4ef6:
\n true <\/em>\u6216
\n false <\/em>\u3002 Java\u4e2d\u6709\u591a\u79cd\u7c7b\u578b\u7684if\u8bed\u53e5\u3002\n <\/div>\n

if\u8bed\u53e5<\/span>
\n if-else \u8bed\u53e5<\/span>
\n if-else-if \u8bed\u53e5<\/span>
\n \u5d4c\u5957if \u8bed\u53e5<\/span> <\/p>\n

Java if\u8bed\u53e5<\/h2>\n
\n Java if\u8bed\u53e5\u6d4b\u8bd5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5b83\u5c06\u6267\u884c
\n if\u5757<\/em>\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 if(condition){
\/\/code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java If-else\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 \/\/Java Program to demonstate the use of if <\/span>statement.public <\/span>class IfExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/defining an 'age'<\/span> variable int age=20;
        \/\/checking the age if(age>
        18){
            System.out.print<\/span>(\"Age is greater than 18\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 Age is greater than 18 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

Java if-else\u8bed\u53e5<\/h2>\n
\n Java if-else\u8bed\u53e5\u4e5f\u6d4b\u8bd5\u6761\u4ef6\u3002\u5982\u679c\u6761\u4ef6\u4e3atrue\uff0c\u5219\u6267\u884c
\n if\u5757<\/em>\uff0c\u5426\u5219\u6267\u884c
\n else\u5757<\/em>\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 if(condition){
\/\/code if <\/span>condition is true<\/span><\/span>}
else{
\/\/code if <\/span>condition is false<\/span>}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java If-else\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 \/\/A Java Program to demonstrate the use of if-else <\/span>statement.
\/\/It is a program of odd and even number.public <\/span>class IfElseExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/defining a variable int number=13;
        \/\/Check if <\/span>the number is divisible by 2 or not if(number%2==0){
            System.out.println<\/span><\/span>(\"even number\"<\/span>);
        }
        else{
            System.out.println<\/span><\/span>(\"odd number\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 odd number <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n Le\u5e74\u793a\u4f8b: <\/strong>\n <\/div>\n
\n \u5982\u679c\u5e74\u4efd\u53ef\u4ee5\u88ab4\u548c400\u6574\u9664\uff0c\u5219\u662fleap\u5e74\uff0c\u4f46\u4e0d\u80fd\u88ab100\u6574\u9664\u3002\n <\/div>\n
\n
 public <\/span>class LeapYearExample {
    public <\/span>static void <\/span>main(String[] args) {
        int year=2020;
        if(((year % 4 ==0) &
        &
        (year % 100 !=0)) ||<\/span> (year % 400==0)){
            System.out.println<\/span><\/span>(\"LEAP YEAR\"<\/span>);
        }
        else{
            System.out.println<\/span><\/span>(\"COMMON YEAR\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 LEAP YEAR <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

\u4f7f\u7528\u4e09\u5143\u8fd0\u7b97\u7b26<\/h2>\n
\n \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e09\u5143\u8fd0\u7b97\u7b26(\uff1f:)\u6765\u6267\u884cif ... else\u8bed\u53e5\u7684\u4efb\u52a1\u3002\u8fd9\u662f\u68c0\u67e5\u60c5\u51b5\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ed3\u679c\u4e3a\uff1f\u8fd4\u56de\u3002\u4f46\u662f\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u8fd4\u56de: \u7684\u7ed3\u679c\u3002\n <\/div>\n
\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 public <\/span>class IfElseTernaryExample {
    public <\/span>static void <\/span>main(String[] args) {
        int number=13;
        \/\/Using ternary operator <\/span> String output=(number%2==0)?\"even number\"<\/span>:\"odd number\"<\/span>;
        System.out.println<\/span><\/span>(output);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 odd number <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

Java if-else-if\u9636\u68af\u8bed\u53e5<\/h2>\n
\n if-else-if\u9636\u68af\u8bed\u53e5\u4ece\u591a\u4e2a\u8bed\u53e5\u4e2d\u6267\u884c\u4e00\u4e2a\u6761\u4ef6\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 if(condition1){
\/\/code to be executed if <\/span>condition1 is true<\/span><\/span>}
else <\/span>if(condition2){
\/\/code to be executed if <\/span>condition2 is true<\/span><\/span>}
else <\/span>if(condition3){
\/\/code to be executed if <\/span>condition3 is true<\/span><\/span>}
...else{
\/\/code to be executed if <\/span>all the conditions are false<\/span>}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java If-else\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 \/\/Java Program to demonstrate the use of if <\/span>else-if <\/span>ladder.
\/\/It is a program of grading system for <\/span>fail, D grade, C grade, B grade, A grade and A+.public <\/span>class IfElseIfExample {
    public <\/span>static void <\/span>main(String[] args) {
        int marks=65;
        if(marks<50){
            System.out.println<\/span><\/span>(\"fail\"<\/span>);
        }
        else <\/span>if(marks>=50 &&marks<60){
            System.out.println<\/span><\/span>(\"D grade\"<\/span>);
        }
        else <\/span>if(marks>=60 &&marks<70){
            System.out.println<\/span><\/span>(\"C grade\"<\/span>);
        }
        else <\/span>if(marks>=70 &&marks<80){
            System.out.println<\/span><\/span>(\"B grade\"<\/span>);
        }
        else <\/span>if(marks>=80 &&marks<90){
            System.out.println<\/span><\/span>(\"A grade\"<\/span>);
        }
        else <\/span>if(marks>=90 &&marks<100){
            System.out.println<\/span><\/span>(\"A+ grade\"<\/span>);
        }
        else{
            System.out.println<\/span><\/span>(\"Invalid!\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 C grade <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n \u68c0\u67e5\u6b63\uff0c\u8d1f\u6216\u96f6\u7684\u7a0b\u5e8f: <\/strong>\n <\/div>\n
\n
 public <\/span>class PositiveNegativeExample {
    public <\/span>static void <\/span>main(String[] args) {
        int number=-13;
        if(number>
        0){
            System.out.println<\/span><\/span>(\"POSITIVE\"<\/span>);
        }
        else <\/span>if(number<
        0){
            System.out.println<\/span><\/span>(\"NEGATIVE\"<\/span>);
        }
        else{
            System.out.println<\/span><\/span>(\"ZERO\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 NEGATIVE <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

Java\u5d4c\u5957\u7684if\u8bed\u53e5<\/h2>\n
\n \u5d4c\u5957\u7684if\u8bed\u53e5\u8868\u793a\u53e6\u4e00\u4e2aif\u5757\u4e2d\u7684
\n if\u5757<\/em>\u3002\u5728\u6b64\uff0c\u4ec5\u5f53\u5916\u90e8if\u5757\u6761\u4ef6\u4e3atrue\u65f6\u624d\u6267\u884c\u5185\u90e8if\u5757\u6761\u4ef6\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 if(condition){
    \/\/code to be executed if(condition){
    \/\/code to be executed }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java If-else\u8bed\u53e5_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n \u793a\u4f8b: <\/strong>\n <\/div>\n
\n
 \/\/Java Program to demonstrate the use of Nested if <\/span>Statement.public <\/span>class JavaNestedIfExample {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/Creating two variables for <\/span>age and weight int age=20;
        int weight=80;
        \/\/applying condition on age and weight if(age>
        =18){
            if(weight>
            50){
                System.out.println<\/span><\/span>(\"You are eligible to donate blood\"<\/span>);
            }
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 You are eligible to donate blood <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n \u793a\u4f8b2: <\/strong>\n <\/div>\n
\n
 \/\/Java Program to demonstrate the use of Nested if <\/span>Statement. public <\/span>class JavaNestedIfExample2 {
    public <\/span>static void <\/span>main(String[] args) {
        \/\/Creating two variables for <\/span>age and weight int age=25;
        int weight=48;
        \/\/applying condition on age and weight if(age>=18){
            if(weight>50){
                System.out.println<\/span><\/span>(\"You are eligible to donate blood\"<\/span>);
            }
            else{
                System.out.println<\/span><\/span>(\"You are not eligible to donate blood\"<\/span>);
            }
        }
        else{
            System.out.println<\/span><\/span>(\"Age must be greater than 18\"<\/span>);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
 You are not eligible to donate blood <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java If-else\u8bed\u53e5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/704"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}