Java基础-程序流程控制第一弹(分支结构/选择结构)

              Java基础-程序流程控制第一弹(分支结构/选择结构)

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.if语句

1>.if语句的第一种格式

1   if(条件表达式){
2 
3     语句体;
4 
5   }
6 
7   ........//其它语句

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 
10 public class JudegmentLetter{
11     public static void main(String[] args){
12         //从键盘读取一个字符,判断这个字符是否为英文字母,a~z以及A-Z.
13         Scanner Input = new Scanner(System.in);
14         System.out.print("请输入一个字符:>>>");
15         String str = Input.next();
16         char firstLetter = str.charAt(0);        //从键盘上去读第一个字符
17         
18         //判断这个字符是否为英文字母
19         if(('A' <= firstLetter && firstLetter <= 'Z')||('a' <= firstLetter && firstLetter <= 'z')){
20             System.out.println("输入了英文字母:"+firstLetter);
21         }
22         
23     }
24 }

 2>.if语句的第二种格式

1   if(条件表达式){
2     语句体1;
3   }else{
4     语句体2;
5   }
6   ........//其它语句

 

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class OddEven{
10     public static void main(String[] args){
11         Scanner Input = new Scanner(System.in);
12         System.out.print("请输入一个整数:>>>>");
13         int num = Input.nextInt();
14     
15         //判断奇数还是偶数
16         if( num % 2 == 0){
17             System.out.println(  num + "是偶数!" );
18         }else{
19             System.out.println( num + "是奇数!" );
20         }
21     }
22 }
 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class UserLogin{
10     public static void main(String[] args){
11         /*
12             从键盘输入用户名和密码,判断是否为合法用户
13             假设合法的用户名是:yinzhengjie
14             假设合法的密  码是:123
15         */
16         Scanner Input = new Scanner(System.in);
17         System.out.print("请输入用户名:>>>");
18         String username = Input.next();                    //从键盘上获取用户名
19         System.out.print("请输入密 码:>>>");
20         String password = Input.next();                    //从键盘上获取密  码
21         
22         //判断用户是否为合法用户
23         if("yinzhengjie".equals(username) && "123".equals(password)){
24             System.out.println("登录成功!");
25         }else{
26             System.out.println("登录失败,用户名或密码不正确!");
27         }
28     }
29 }
用户登录脚本,难点是在于字符串比较的方式!
 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class BuildTriangle{
10     public static void main(String[] args){
11         /*
12             判断输入的三个数字能够构成三角形
13         */
14         
15         Scanner Input = new Scanner(System.in);
16         System.out.print("Please enter the length:>>>");
17         int length = Input.nextInt();
18         System.out.print("Please input the width:>>>");
19         int width = Input.nextInt();
20         System.out.print("Please enter high:>>>");
21         int high = Input.nextInt();
22         
23         
24         if( length + width > high && length + high > width && width + high > length){
25             System.out.println("三边" + length + "," + width + "," + high + "能构成三角形");
26         }else{
27             System.out.println("三边" + length + "," + width + "," + high + "不能构成三角形");
28         }
29         
30         
31     }
32 }
判断输入的三个数字能够构成三角形

3>.if语句的第三种格式

 1   if(条件表达式1){
 2     语句体1;
 3   }else if(条件表达式2){
 4     语句体2;
 5   }else if(条件表达式3){
 6     语句体3;
 7   }
 8      .......
 9   else{
10     语句体n;
11   }
12   .......//其它语句

   接下来,我们一起看一个案例,判断成绩对应的等级,代码如下:

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class ExamResults{
10     public static void main(String[] args){
11         /*
12             从键盘输入一个成绩,判断成绩对应的等级。
13         */
14         Scanner Input = new Scanner(System.in);
15         System.out.print("请输入一个成绩:>>>");
16         int score = Input.nextInt();
17         
18         //先对用户输入的成绩进行合理性验证,把不合理的抛去
19         if( score < 0 || score > 100 ){
20             System.out.println("你输入的成绩不合理,应该在0~100之间");
21             return;        //返回,结束当前方法main方法的执行。
22         }
23         
24         //判断成绩对应的等级,如果程序能跑到这,隐性的说明成绩在0~100之间。
25         if( score >= 90 && score <= 100 ){
26             System.out.println("优秀!");
27         }else if( score >= 80 ){
28             System.out.println("良好!");
29         }else if( score >= 70 ){
30             System.out.println("一般!");
31         }else if( score >= 60 ){
32             System.out.println("得加油了!");
33         }else if( score >= 0 ){
34             System.out.println("不及格!");
35         }
36         
37     }
38 }

 

二.switch语句

1>.语法格式

 1 switch(表达式或变量){
 2   case 取值1:
 3   语句体1;
 4   break;
 5 case 取值2:
 6   语句体2;  
 7   break;
 8  9 default:
10   语句体n+111   break;
12 }

2>.流程图

 

3>.案例展示

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class WeekJudge{
10     public static void main(String[] args){
11         /*
12             从键盘输入一个1~7之间的数字,输出这个数字对应星期几。
13         */
14         
15         Scanner Input = new Scanner(System.in);
16         
17         System.out.print("请输入一个1~7之间的数字");
18         int num = Input.nextInt();
19         
20         String weekday     ;        //这里声明一个变量是用来保存数字对应的星期几
21         
22         /**
23         //用if语句实现:
24         if( num == 1 ){
25             weekday = "星期一";
26         }else if ( num == 2 ){
27             weekday = "星期二";
28         }else if ( num == 3 ){
29             weekday = "星期三";
30         }else if ( num == 4 ){
31             weekday = "星期四";
32         }else if ( num == 5 ){
33             weekday = "星期五";
34         }else if ( num == 6 ){
35             weekday = "星期六";
36         }else if ( num == 7 ){
37             weekday = "星期日";
38         }else {
39             weekday = "你输入的数字不合法";
40         }
41         */
42         
43         //用switch语句实现:
44         switch( num ){
45             case 1:
46                 weekday = "星期一";
47                 break;
48             case 2:
49                 weekday = "星期二";
50                 break;
51             case 3:
52                 weekday = "星期三";
53                 break;
54             case 4:
55                 weekday = "星期四";
56                 break;
57             case 5:
58                 weekday = "星期五";
59                 break;
60             case 6:
61                 weekday = "星期六";
62                 break;
63             case 7:
64                 weekday = "星期日";
65                 break;
66             default:
67                 weekday = "你输入的数字不合法";
68         }
69         System.out.println( weekday);
70         
71     }
72 }

4>.case的穿透

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class Quarter{
10     public static void main(String[] args){
11         /*
12             从键盘输入月份数字,显示是第几个季度
13         */
14         Scanner s = new Scanner(System.in);
15         System.out.print("请输入月份:");
16         int month = s.nextInt();
17         
18         switch(month){
19             case 1:{}
20             case 2:{}
21             case 3:{
22                 System.out.println(month + "属于第一季度");
23                 break;
24             }
25             case 4:{}
26             case 5:{}
27             case 6:{
28                 System.out.println(month + "属于第二季度");
29                 break;
30             }
31             case 7:{}
32             case 8:{}
33             case 9:{
34                 System.out.println(month + "属于第三季度");
35                 break;
36             }
37             case 10:{}
38             case 11:{}
39             case 12:{
40                 System.out.println(month + "属于第四季度");
41                 break;
42             }
43             default :{
44                 System.out.println("输入的月份有误");
45                 break;
46             }
47         }
48         
49     }
50 }

5>.switch语句注意事项

 

  a>.switch语句表达式的类型有:byte,short,int,char,String(1.7之后才支持)和枚举类型;

 

  b>.case之间与default没有顺序:case相当于入口点,没有匹配的case将执行default,default也不是必须的;

 

  c>.结束switch语句的情况:遇到break或执行到switch语句结束;

 

  d>.如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾;

 

  e>.case 后面必须跟的是常量表达且各个case常量值不能重复。

 

 

三.小试牛刀

1>.计算当前的时间

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class CurrentTime{
10     public static void main(String[] args){
11         /*
12             使用System.currentTimeMillis() 计算当前时间;System.currentTimeMillis() 可以
13         返回当前时间与协调世界时 1970 年 1 月1 日物业之间的时间。
14         */
15         
16         long sumMillis = System.currentTimeMillis();    //把1970.1.1午夜的毫秒数保存到一个变量中
17         
18         long sumSeconds = sumMillis/1000;                //总的秒数
19         
20         long currentSecond = sumSeconds%60;            //当前的秒数
21         
22         long sumMinutes = sumSeconds / 60;                //总的分钟数
23         
24         long currentMinute = sumMinutes % 60;            //当前的分钟数
25         
26         long sumHours = sumMinutes / 60;                //总的小时数
27         
28         long currentHour = sumHours % 24;                //当前的小时数
29         
30         long beijingHour = (currentHour + 8) % 24;        //计算北京的时间,注意,UTC时间和北京时间有时差为8小时。
31         
32         System.out.println(beijingHour + ":" + currentMinute + ":" + currentSecond);
33         
34     }
35 }

2>.甲计算两个点的距离

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:y1053419035@qq.com
 5 */
 6 
 7 import java.util.Scanner;
 8 
 9 public class PointDistance{
10     public static void main(String[] args){
11     /*
12         从键盘上输入两个点的坐标,计算这两个点的距离,Math.pow(a,b)返回a的b次方
13     */
14     Scanner Input = new Scanner(System.in);
15     System.out.print("请输入第一个点的坐标:>>>");
16     int x1 = Input.nextInt();
17     int y1 = Input.nextInt();
18     System.out.print("请输入第二个点的坐标:>>>");
19     int x2 = Input.nextInt();
20     int y2 = Input.nextInt();
21     
22     int temp = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
23     
24     double distance = Math.pow( temp,0.5 );
25     
26     System.out.println("这两个点的距离为:" + distance );
27     
28     }
29 }

3>.

posted @ 2018-04-02 19:25  尹正杰  阅读(503)  评论(0编辑  收藏  举报