java学习之路 之 基本语法-程序流程控制-(if-else)语句练习题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qintian888/article/details/53968164
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class IfTest {  
  2.       
  3.     public static void main(String[] args) {  
  4.         int a = 200;  
  5.         /* 
  6.         if (布尔表达式) { 
  7.             语句块 // 要想执行必须布尔表达式为真 
  8.         }*/  
  9.         if (a == 20) {  
  10.             System.out.println("a==20"); // 有条件地执行  
  11.         }  
  12.           
  13.         System.out.println("after if...");  
  14.     }  
  15. }  
  16.   
  17. class IfTest2 {  
  18.       
  19.     public static void main(String[] args) {  
  20.         int a = 20;  
  21.         if (a == 200) {  
  22.             System.out.println("a==20");  
  23.         } else { // 否则, 如果if中的条件为假, 执行下面的语句  
  24.             System.out.println("else");  
  25.         }  
  26.           
  27.         System.out.println("after if else ...");  
  28.     }  
  29. }  
  30.   
  31. class IfTest3 {  
  32.       
  33.     // 分支 : 多个条件, 只允许有一个执行  
  34.     public static void main(String[] args) {  
  35.         int a = 20;  
  36.         if (a == 10) {  
  37.             System.out.println("a==10");  
  38.         } else if (a == 20) {  
  39.             System.out.println("a==20");  
  40.         } else if (a == 30) {  
  41.             System.out.println("a==30");  
  42.         } else {  
  43.             System.out.println("else");  
  44.         }  
  45.         System.out.println("after if else if ...");  
  46.     }  
  47. }  
  48.   
  49. class Exer3 {  
  50.       
  51.     public static void main(String[] args) {  
  52.         /* 
  53.         从命令行参数接收小明的期末成绩。 
  54.         当成绩为100分时,奖励一辆BMW; 
  55.         当成绩为(80,99]时,奖励一个台iphone6s; 
  56.         if (80 < score <= 99) 
  57.         当成绩为[60,80]时,奖励一本参考书; 
  58.         其它时,什么奖励也没有。 
  59.         提示: 从命令行参数获取整数的代码: 
  60.         int score = Integer.parseInt(args[0]); 
  61.         */  
  62.         int score = Integer.parseInt(args[0]);  
  63.         if (score > 100 || score < 0) {  
  64.             System.out.println("输入的数据非法");  
  65.         } else if (score == 100) {  
  66.             System.out.println("奖励一辆BMW");  
  67.         } else if (score > 80 && score <= 99) {  
  68.             System.out.println("奖励一个台iphone6s");  
  69.         } else if (score >= 60 && score <= 80) {  
  70.             System.out.println("奖励一本参考书");  
  71.         } else {  
  72.             System.out.println("什么奖励也没有");  
  73.         }  
  74.     }  
  75. }  
  76.   
  77. /* 
  78. 大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:高:180cm以上;富:财富1千万以上;帅:是。 
  79. 如果这三个条件同时满足,则:“我一定要嫁给他!!!” 
  80. 如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。” 
  81. 如果三个条件都不满足,则:“不嫁!” 
  82. */  
  83. public class IfTest {  
  84.       
  85.     public static void main(String[] args) {  
  86.         int heigh = Integer.parseInt(args[0]);  
  87.         int money = Integer.parseInt(args[1]);  
  88.         boolean b = Boolean.parseBoolean(args[2]);  
  89.           
  90.         if (heigh > 180 && money > 1000 && b == true) {  
  91.             System.out.println("我一定要嫁给他!!!");  
  92.         } else if (heigh > 180 || money > 1000 || b == true) {  
  93.             System.out.println("嫁吧,比上不足,比下有余");  
  94.         } else {  
  95.             System.out.println("不嫁!");  
  96.         }  
  97.     }  
  98. }  
  99.   
  100. //编写程序:接收三个命令行字符串并转换为整数分别存入变量num1、num2、num3,对它们  
  101. //进行排序(使用 if-else if-else),并且从小到大输出。  
  102. public class IfTest1 {  
  103.       
  104.     public static void main(String[] args) {  
  105.         int num1 = Integer.parseInt(args[0]);  
  106.         int num2 = Integer.parseInt(args[1]);  
  107.         int num3 = Integer.parseInt(args[2]);  
  108.           
  109.         if (num1 > num2){  
  110.             if (num3 > num1){  
  111.                 System.out.println(num2 + "," + num1 + "," + num3);  
  112.             } else if (num2 > num3) {  
  113.                 System.out.println(num3 + "," + num2 + "," + num1);  
  114.             } else{  
  115.                 System.out.println(num2 + "," + num3 + "," + num1);  
  116.             }  
  117.         }else {  
  118.             if (num3 > num2) {  
  119.                 System.out.println(num1 + "," + num2 + "," + num3);  
  120.             }else if(num1 > num3) {  
  121.                 System.out.println(num3 + "," + num1 + "," + num2);  
  122.             }else {  
  123.                 System.out.println(num1 + "," + num3 + "," + num2);  
  124.             }  
  125.         }  
  126.     }  
  127. }  
  128.   
  129. class IfTest11 {  
  130.     public static void main(String[] args) {  
  131.         int num1 = Integer.parseInt(args[0]);  
  132.         int num2 = Integer.parseInt(args[1]);  
  133.         int num3 = Integer.parseInt(args[2]);  
  134.         //比较交换3次  
  135.         //1和2  
  136.         if (num1 > num2) {  
  137.             int tmp = num1;  
  138.             num1 = num2;  
  139.             num2 = tmp;  
  140.         }  
  141.         //2和3  
  142.         if (num2 > num3) {  
  143.             int tmp = num2;  
  144.             num2 = num3;  
  145.             num3 = tmp;  
  146.         }  
  147.         //1和2  
  148.         if (num1 > num2) {  
  149.             int tmp = num1;  
  150.             num1 = num2;  
  151.             num2 = tmp;  
  152.         }  
  153.         System.out.println(num1 + "," + num2 + "," + num3);  
  154.     }     
  155. }  
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class IfTest {  
  2.       
  3.     public static void main(String[] args) {  
  4.         int a = 200;  
  5.         /* 
  6.         if (布尔表达式) { 
  7.             语句块 // 要想执行必须布尔表达式为真 
  8.         }*/  
  9.         if (a == 20) {  
  10.             System.out.println("a==20"); // 有条件地执行  
  11.         }  
  12.           
  13.         System.out.println("after if...");  
  14.     }  
  15. }  
  16.   
  17. class IfTest2 {  
  18.       
  19.     public static void main(String[] args) {  
  20.         int a = 20;  
  21.         if (a == 200) {  
  22.             System.out.println("a==20");  
  23.         } else { // 否则, 如果if中的条件为假, 执行下面的语句  
  24.             System.out.println("else");  
  25.         }  
  26.           
  27.         System.out.println("after if else ...");  
  28.     }  
  29. }  
  30.   
  31. class IfTest3 {  
  32.       
  33.     // 分支 : 多个条件, 只允许有一个执行  
  34.     public static void main(String[] args) {  
  35.         int a = 20;  
  36.         if (a == 10) {  
  37.             System.out.println("a==10");  
  38.         } else if (a == 20) {  
  39.             System.out.println("a==20");  
  40.         } else if (a == 30) {  
  41.             System.out.println("a==30");  
  42.         } else {  
  43.             System.out.println("else");  
  44.         }  
  45.         System.out.println("after if else if ...");  
  46.     }  
  47. }  
  48.   
  49. class Exer3 {  
  50.       
  51.     public static void main(String[] args) {  
  52.         /* 
  53.         从命令行参数接收小明的期末成绩。 
  54.         当成绩为100分时,奖励一辆BMW; 
  55.         当成绩为(80,99]时,奖励一个台iphone6s; 
  56.         if (80 < score <= 99) 
  57.         当成绩为[60,80]时,奖励一本参考书; 
  58.         其它时,什么奖励也没有。 
  59.         提示: 从命令行参数获取整数的代码: 
  60.         int score = Integer.parseInt(args[0]); 
  61.         */  
  62.         int score = Integer.parseInt(args[0]);  
  63.         if (score > 100 || score < 0) {  
  64.             System.out.println("输入的数据非法");  
  65.         } else if (score == 100) {  
  66.             System.out.println("奖励一辆BMW");  
  67.         } else if (score > 80 && score <= 99) {  
  68.             System.out.println("奖励一个台iphone6s");  
  69.         } else if (score >= 60 && score <= 80) {  
  70.             System.out.println("奖励一本参考书");  
  71.         } else {  
  72.             System.out.println("什么奖励也没有");  
  73.         }  
  74.     }  
  75. }  
  76.   
  77. /* 
  78. 大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:高:180cm以上;富:财富1千万以上;帅:是。 
  79. 如果这三个条件同时满足,则:“我一定要嫁给他!!!” 
  80. 如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。” 
  81. 如果三个条件都不满足,则:“不嫁!” 
  82. */  
  83. public class IfTest {  
  84.       
  85.     public static void main(String[] args) {  
  86.         int heigh = Integer.parseInt(args[0]);  
  87.         int money = Integer.parseInt(args[1]);  
  88.         boolean b = Boolean.parseBoolean(args[2]);  
  89.           
  90.         if (heigh > 180 && money > 1000 && b == true) {  
  91.             System.out.println("我一定要嫁给他!!!");  
  92.         } else if (heigh > 180 || money > 1000 || b == true) {  
  93.             System.out.println("嫁吧,比上不足,比下有余");  
  94.         } else {  
  95.             System.out.println("不嫁!");  
  96.         }  
  97.     }  
  98. }  
  99.   
  100. //编写程序:接收三个命令行字符串并转换为整数分别存入变量num1、num2、num3,对它们  
  101. //进行排序(使用 if-else if-else),并且从小到大输出。  
  102. public class IfTest1 {  
  103.       
  104.     public static void main(String[] args) {  
  105.         int num1 = Integer.parseInt(args[0]);  
  106.         int num2 = Integer.parseInt(args[1]);  
  107.         int num3 = Integer.parseInt(args[2]);  
  108.           
  109.         if (num1 > num2){  
  110.             if (num3 > num1){  
  111.                 System.out.println(num2 + "," + num1 + "," + num3);  
  112.             } else if (num2 > num3) {  
  113.                 System.out.println(num3 + "," + num2 + "," + num1);  
  114.             } else{  
  115.                 System.out.println(num2 + "," + num3 + "," + num1);  
  116.             }  
  117.         }else {  
  118.             if (num3 > num2) {  
  119.                 System.out.println(num1 + "," + num2 + "," + num3);  
  120.             }else if(num1 > num3) {  
  121.                 System.out.println(num3 + "," + num1 + "," + num2);  
  122.             }else {  
  123.                 System.out.println(num1 + "," + num3 + "," + num2);  
  124.             }  
  125.         }  
  126.     }  
  127. }  
  128.   
  129. class IfTest11 {  
  130.     public static void main(String[] args) {  
  131.         int num1 = Integer.parseInt(args[0]);  
  132.         int num2 = Integer.parseInt(args[1]);  
  133.         int num3 = Integer.parseInt(args[2]);  
  134.         //比较交换3次  
  135.         //1和2  
  136.         if (num1 > num2) {  
  137.             int tmp = num1;  
  138.             num1 = num2;  
  139.             num2 = tmp;  
  140.         }  
  141.         //2和3  
  142.         if (num2 > num3) {  
  143.             int tmp = num2;  
  144.             num2 = num3;  
  145.             num3 = tmp;  
  146.         }  
  147.         //1和2  
  148.         if (num1 > num2) {  
  149.             int tmp = num1;  
  150.             num1 = num2;  
  151.             num2 = tmp;  
  152.         }  
  153.         System.out.println(num1 + "," + num2 + "," + num3);  
  154.     }     
  155. }  

猜你喜欢

转载自blog.csdn.net/qintian888/article/details/53968164