{"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
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 if\u5757<\/em>\u3002\n <\/div>\n if(condition){
\/\/code to be executed}
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/p>\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 Age is greater than 18 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
Java if-else\u8bed\u53e5<\/h2>\n
\n if\u5757<\/em>\uff0c\u5426\u5219\u6267\u884c
\n else\u5757<\/em>\u3002\n <\/div>\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 <\/p>\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 odd number <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\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 LEAP YEAR <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\u4f7f\u7528\u4e09\u5143\u8fd0\u7b97\u7b26<\/h2>\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 odd number <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
Java if-else-if\u9636\u68af\u8bed\u53e5<\/h2>\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 <\/p>\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 C grade <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\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 NEGATIVE <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
Java\u5d4c\u5957\u7684if\u8bed\u53e5<\/h2>\n
\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 if(condition){
\/\/code to be executed if(condition){
\/\/code to be executed }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/p>\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 You are eligible to donate blood <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\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>);