if else 选择机构 _多重if选择机构_if选择结构嵌套(综合练习题——code)

  1 import java.util.*;
  2 class If01{
  3     public static void main(String[ ]args){
  4         //练习1:假如张三参加Java考试,判断如果在95分以上则有奖励
  5         Scanner input =new Scanner(System.in);
  6         System.out.print("请输入张三的考试成绩:");
  7         double score =input.nextDouble();
  8         /*if (score > 95) {
  9             System.out.println("有奖励");
 10         }*/
 11 
 12         //注意:在java中,当代码块只有一条语句时,则大括号可以省略,建议初学者编写大括号
 13     if (score > 95)                                                                
 14             System.out.println("有奖励");                        
 15         System.out.println("有惩罚");
 16     /*=      if (score > 95)  {                                                             
 17                         System.out.println("有奖励");
 18                }
 19                System.out.println("有惩罚");    */
 20     }
 21 }  
 22 class If02{
 23     public static void main(String[ ]args){
 24         //练习2:假如张三参加Java考试,判断如果在95分以上则有奖励,否则发红包
 25         Scanner input = new Scanner(System.in);
 26         System.out.print("请输入成绩: ");
 27         double score = input.nextDouble();
 28         //第一种方法:使用两个if块完成的
 29         /*if (score > 95){
 30             System.out.println("有奖励");
 31         }
 32         if (score <=95){
 33             System.out.println("发红包");
 34         }*/
 35         
 36         //第二种方法:使用if else完成
 37         /*if(score >95) {
 38             System.out.println("有奖励");
 39         } else{
 40             System.out.println("发红包");    
 41         }*/
 42         /*第三种方法:使用三目运算符完成
 43         System.out.println(score >95 ?    "有奖励"    :    "发红包");*/
 44         //第四种方法:使用多重if完成
 45         if(score > 95){
 46             System.out.println("有奖励");
 47         }else if(score <= 95){
 48             System.out.println("发红包");
 49         }
 50     }
 51 }
 52 class If03{
 53     public static void main(String[ ]args){
 54         //练习3:判断一个数是否是三位的正数
 55         Scanner input = new Scanner(System.in);
 56         System.out.print("请输入需要输入的数:");
 57         int num = input.nextInt();
 58         if (num >100 && num < 1000) {
 59             System.out.println(num +"是正数");
 60         } else{
 61             System.out.println(num +"不是正数");
 62         }
 63     }
 64 }
 65     
 66 class If04{
 67     public static void main(String[ ]args){
 68         //练习4:输入两个数,分别存放在a和b中,判断a+b的和大于100,则输出a的值,否则输出b的值
 69         Scanner input = new Scanner(System.in);
 70         System.out.print("请输入数字 :");
 71         int a = input.nextInt();
 72         System.out.print("请输入数字 :");
 73         int b = input.nextInt();
 74         System.out.println("a = " + a + "
b = " +b);
 75         if (a + b > 100){
 76             System.out.println("输出:a =" +a);
 77         }else{
 78             System.out.println("输出b = " +b);
 79         }
 80     }
 81 }
 82 
 83 class If05{
 84     public static void main(String[ ]args){
 85         //练习5:判断一个数是奇数还是偶数(使用多重if选择结构完成此练习)
 86         Scanner input = new Scanner(System.in);
 87         System.out.print("请输入你想要输入的数字:");
 88         int num = input.nextInt();
 89         if(num % 2 == 0){
 90             System.out.println("偶数");
 91         } else{
 92             System.out.println("奇数");
 93         }
 94     }
 95 }
 96 
 97 class If06{
 98     public static void main(String[ ]args){
 99         //练习6:根据输入的年龄,输出是老年(55以上)、中年(18-54)、青年(18-29)还是少年(0----17)
100         Scanner input = new Scanner(System.in);
101         System.out.print("请输入年龄: ");
102         int age = input.nextInt();
103         /*if (age >55 ){
104             System.out.println("老年");
105         }else if(age >= 18){
106             System.out.println("青年");
107         }else if(age >= 30){
108             System.out.println("中年");
109         }else if(age <18){
110             System.out.println("少年");
111         }*/
112 
113         //注意:多重if选择结构种的条件顺序可以颠倒,但是可能影响运行结果
114         if (age >55 ){
115             System.out.println("老年");
116         }else if(age >= 18 && age<=29){
117             System.out.println("青年");
118         }else if(age >= 30){
119             System.out.println("中年");
120         }else if(age <18 && age >=0){
121             System.out.println("少年");
122         }else{
123             System.out.println("输入年龄有误!");
124         }
125     }
126 }
127 
128 class If07{
129     public static void main(String[ ]args){
130         //练习7:判断一个字符,输出是大写字母、小写字母还是数字字符
131     }
132 }
133 
134 class If08{
135     public static void main(String[ ]args){
136         //练习8:判断一个两位数,是奇数还是偶数
137         Scanner input = new Scanner(System.in);
138         System.out.print("请输入一个数:");
139         int num = input.nextInt();
140         //判断,当前数num是否是两位数
141         if (num >=10 && num<=99){
142             //判断,当前数是奇数还是偶数
143             if(num %2 ==0){
144                 System.out.println(num+"是偶数");
145             }else{
146                 System.out.println(num+"是奇数");
147             }
148         }else{
149             System.out.println(num + "不是两位数");
150         }
151     }
152 }
153 
154 class If09{
155     public static void main(String[ ]args){
156         //练习9:判断一个三位正整数,是否是水仙花数
157         Scanner input = new Scanner(System.in);
158         System.out.print("请输入数字: ");
159         int  num = input.nextInt();
160         //判断当前数是否是三位正整数
161         if (num>=100 && num <=999){        //num = 123
162             //获取各个位数
163             int bw = num / 100 , sw = num % 100 / 10 , gw = num % 10;
164             //计算各个位的立方和
165             int sum = bw*bw*bw+sw*sw*sw+gw*gw*gw;
166             //判断,各个位立方和是否与当前数num相等
167             if (num == sum){
168                 System.out.println(num+"是水仙花数");
169             }else{
170                 System.out.println(num+"不是水仙花数");
171             }
172         }else{
173             System.out.println(num+"不是三位正整数");
174         }
175     }
176 }
坎坷困难会让你不断的强大起来 -- 前提是你别怂
原文地址:https://www.cnblogs.com/penphy/p/10486685.html