目录
java的 一个 if 语句包含一个布尔表达式和一条或多条语句。
if语句
语法结构:
if(布尔表达式)
{
//如果布尔表达式为true将执行的语句
}
如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。
实例1:
public class Test {
public static void main(String args[]) {
int a = 100;
int b = 2000;
if (a == 100 || b == 1000) {
System.out.print("a = 100 或者 b = 1000 。");
}
}
}
结果展示1:
a = 100 或者 b = 1000 。
实例2:
public class Test {
public static void main(String args[]){
int [] numbers = {1, 2, 3, 4, 5, 6};
for(int a : numbers ){
if(a > 4){
break;
}
System.out.print( a );
System.out.print(",");
}
System.out.print("\n");
String [] typenames ={"tall", "short", "long", "fat"};
for( String tp : typenames ) {
System.out.print( tp );
System.out.print(",");
}
}
}
结果展示2:
1,2,3,4,
tall,short,long,fat,
if...else语句
if 语句后面可以跟 else 语句,当 if 语句的布尔表达式值为 false 时,else 语句块会被执行。
语法结构:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
实例1:
public class Test {
public static void main(String args[]) {
int a = 100;
int b = 2000;
if (a == 100 && b == 1000) {
System.out.print("a = 100 并且 b = 1000 。");
} else {
System.out.print("a != 100 或 b != 1000 。");
}
}
}
结果展示1:
a != 100 或 b != 1000 。
实例2:
public class Test {
public static void main(String args[]){
int [] numbers = {1, 2, 3, 4, 5, 6};
for(int a : numbers ){
if(a > 4){
System.out.print( "a大于4,a - 4 =" );
System.out.print( a - 4 );
}else{
System.out.print( "a小于4,a + 4 =" );
System.out.print( a + 4 );
}
System.out.print("\n");
}
System.out.print("\n");
String [] typenames ={"tall", "short", "long", "fat"};
for( String tp : typenames ) {
System.out.print( tp );
System.out.print(",");
}
}
}
结果展示2:
a小于4,a + 4 =5
a小于4,a + 4 =6
a小于4,a + 4 =7
a小于4,a + 4 =8
a大于4,a - 4 =1
a大于4,a - 4 =2
if...else if...else 语句
if 语句后面可以跟 else if…else 语句,这种语句可以检测到多种可能的情况。使用时需要注意下面几点:
- if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
- if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
- 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。
语法结构:
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
实例1:
public class Test {
public static void main(String args[]){
int [] numbers = {0, 1, 2, 3, 4, 5, 6};
for(int a : numbers ){
if(a == 4){
System.out.print( "a等于4,a - 4 =" );
System.out.print( a - 4 );
System.out.print("\n");
}else if(a == 0){
System.out.print( "a等于0,a + 4 =" );
System.out.print( a + 4 );
System.out.print("\n");
}
}
System.out.print("\n");
String [] typenames ={"tall", "short", "long", "fat"};
for( String tp : typenames ) {
System.out.print( tp + "的收个字母是:" + tp.charAt(0) );
System.out.print("\n");
}
}
}
结果展示1:
a等于0,a + 4 =4
a等于4,a - 4 =0
tall的收个字母是:t
short的收个字母是:s
long的收个字母是:l
fat的收个字母是:f
实例2:
public class Test {
public static void main(String args[]){
int [] numbers = {0, 1, 2, 3, 4, 5, 6};
for(int a : numbers ){
if(a == 4){
System.out.print( "a等于4,a - 4 =" );
System.out.print( a - 4 );
System.out.print("\n");
}else if(a == 0){
System.out.print( "a等于0,a + 4 =" );
System.out.print( a + 4 );
System.out.print("\n");
}else{
System.out.print( "a不等于0,也不等于4,a = " );
System.out.print( a );
System.out.print("\n");
}
}
System.out.print("\n");
String [] typenames ={"tall", "short", "long", "fat"};
for( String tp : typenames ) {
System.out.print( tp + "的收个字母是:" + tp.charAt(0) );
System.out.print("\n");
}
}
}
结果展示2:
a等于0,a + 4 =4
a不等于0,也不等于4,a = 1
a不等于0,也不等于4,a = 2
a不等于0,也不等于4,a = 3
a等于4,a - 4 =0
a不等于0,也不等于4,a = 5
a不等于0,也不等于4,a = 6
tall的收个字母是:t
short的收个字母是:s
long的收个字母是:l
fat的收个字母是:f
嵌套的 if…else 语句
使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。
语法结构:
if(布尔表达式 1){
////如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){
////如果布尔表达式 2的值为true执行代码
}
}
实例1:
public class Test {
public static void main(String args[]){
int a = 100;
int b = 1000;
if( a == 100 ){
if( b == 1000 ){
System.out.print("a = 100 , b = 1000 。");
}
}
}
}
结果展:1:
a = 100 , b = 1000 。
实例2:
public class Test {
public static void main(String args[]){
int a = 200;
int b = 1000;
if( a == 100 ){
if( b == 1000 ){
System.out.print("a = 100 , b = 1000 。");
}
}else{
System.out.print("a != 100 或 b != 1000 。");
}
}
}
结果展示2:
a != 100 或 b != 1000 。
实例3:
public class Test {
public static void main(String args[]) {
int[] numbers = {0, 1, 2, 3, 4, 5, 6};
String[] typenames = {"tall", "short", "long", "fat"};
for (int a : numbers) {
for (String tp : typenames) {
if (a < 3) {
if (tp.length() == 4) {
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于4");
}else if (tp.length() == 5){
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于5");
}else{
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于3");
}
System.out.print("\n");
}
}
}
}
}
结果展示3:
a小于3,a = 0;tall,字符串长度等于4
a小于3,a = 0;short,字符串长度等于5
a小于3,a = 0;long,字符串长度等于4
a小于3,a = 0;fat,字符串长度等于3
a小于3,a = 1;tall,字符串长度等于4
a小于3,a = 1;short,字符串长度等于5
a小于3,a = 1;long,字符串长度等于4
a小于3,a = 1;fat,字符串长度等于3
a小于3,a = 2;tall,字符串长度等于4
a小于3,a = 2;short,字符串长度等于5
a小于3,a = 2;long,字符串长度等于4
a小于3,a = 2;fat,字符串长度等于3
实例4:
public class Test {
public static void main(String args[]) {
int[] numbers = {0, 1, 2, 3, 4, 5, 6};
String[] typenames = {"tall", "short", "long", "fat"};
for (int a : numbers) {
for (String tp : typenames) {
if (a < 3) {
if (tp.length() == 4) {
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于4");
}else if (tp.length() == 5){
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于5");
}else{
System.out.print("a小于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp + ",字符串长度等于3");
}
System.out.print("\n");
}else{
System.out.print("a大于等于3,a = ");
System.out.print(a);
System.out.print(";");
System.out.print(tp);
System.out.print("\n");
}
}
}
}
}
结果展示4;
a小于3,a = 0;tall,字符串长度等于4
a小于3,a = 0;short,字符串长度等于5
a小于3,a = 0;long,字符串长度等于4
a小于3,a = 0;fat,字符串长度等于3
a小于3,a = 1;tall,字符串长度等于4
a小于3,a = 1;short,字符串长度等于5
a小于3,a = 1;long,字符串长度等于4
a小于3,a = 1;fat,字符串长度等于3
a小于3,a = 2;tall,字符串长度等于4
a小于3,a = 2;short,字符串长度等于5
a小于3,a = 2;long,字符串长度等于4
a小于3,a = 2;fat,字符串长度等于3
a大于等于3,a = 3;tall
a大于等于3,a = 3;short
a大于等于3,a = 3;long
a大于等于3,a = 3;fat
a大于等于3,a = 4;tall
a大于等于3,a = 4;short
a大于等于3,a = 4;long
a大于等于3,a = 4;fat
a大于等于3,a = 5;tall
a大于等于3,a = 5;short
a大于等于3,a = 5;long
a大于等于3,a = 5;fat
a大于等于3,a = 6;tall
a大于等于3,a = 6;short
a大于等于3,a = 6;long
a大于等于3,a = 6;fat
举的实例3和4代码过于繁琐了,才开始学习,见谅啊!