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

时间:2022-06-07 13:21:25

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

                                   作者:尹正杰

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

一.if语句

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

   if(条件表达式){

     语句体;

   }

   ........//其它语句

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

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

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

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

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

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class OddEven{
public static void main(String[] args){
Scanner Input = new Scanner(System.in);
System.out.print("请输入一个整数:>>>>");
int num = Input.nextInt(); //判断奇数还是偶数
if( num % 2 == 0){
System.out.println( num + "是偶数!" );
}else{
System.out.println( num + "是奇数!" );
}
}
}
 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class UserLogin{
public static void main(String[] args){
/*
从键盘输入用户名和密码,判断是否为合法用户
假设合法的用户名是:yinzhengjie
假设合法的密 码是:123
*/
Scanner Input = new Scanner(System.in);
System.out.print("请输入用户名:>>>");
String username = Input.next(); //从键盘上获取用户名
System.out.print("请输入密 码:>>>");
String password = Input.next(); //从键盘上获取密 码 //判断用户是否为合法用户
if("yinzhengjie".equals(username) && "123".equals(password)){
System.out.println("登录成功!");
}else{
System.out.println("登录失败,用户名或密码不正确!");
}
}
}

用户登录脚本,难点是在于字符串比较的方式!

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class BuildTriangle{
public static void main(String[] args){
/*
判断输入的三个数字能够构成三角形
*/ Scanner Input = new Scanner(System.in);
System.out.print("Please enter the length:>>>");
int length = Input.nextInt();
System.out.print("Please input the width:>>>");
int width = Input.nextInt();
System.out.print("Please enter high:>>>");
int high = Input.nextInt(); if( length + width > high && length + high > width && width + high > length){
System.out.println("三边" + length + "," + width + "," + high + "能构成三角形");
}else{
System.out.println("三边" + length + "," + width + "," + high + "不能构成三角形");
} }
}

判断输入的三个数字能够构成三角形

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

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

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

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

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class ExamResults{
public static void main(String[] args){
/*
从键盘输入一个成绩,判断成绩对应的等级。
*/
Scanner Input = new Scanner(System.in);
System.out.print("请输入一个成绩:>>>");
int score = Input.nextInt(); //先对用户输入的成绩进行合理性验证,把不合理的抛去
if( score < 0 || score > 100 ){
System.out.println("你输入的成绩不合理,应该在0~100之间");
return; //返回,结束当前方法main方法的执行。
} //判断成绩对应的等级,如果程序能跑到这,隐性的说明成绩在0~100之间。
if( score >= 90 && score <= 100 ){
System.out.println("优秀!");
}else if( score >= 80 ){
System.out.println("良好!");
}else if( score >= 70 ){
System.out.println("一般!");
}else if( score >= 60 ){
System.out.println("得加油了!");
}else if( score >= 0 ){
System.out.println("不及格!");
} }
}

二.switch语句

1>.语法格式

 switch(表达式或变量){
  case 取值1:
  语句体1;
  break;
case 取值2:
  语句体2;  
  break;

default:
  语句体n+;
  break;
}

2>.流程图

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

3>.案例展示

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class WeekJudge{
public static void main(String[] args){
/*
从键盘输入一个1~7之间的数字,输出这个数字对应星期几。
*/ Scanner Input = new Scanner(System.in); System.out.print("请输入一个1~7之间的数字");
int num = Input.nextInt(); String weekday ; //这里声明一个变量是用来保存数字对应的星期几 /**
//用if语句实现:
if( num == 1 ){
weekday = "星期一";
}else if ( num == 2 ){
weekday = "星期二";
}else if ( num == 3 ){
weekday = "星期三";
}else if ( num == 4 ){
weekday = "星期四";
}else if ( num == 5 ){
weekday = "星期五";
}else if ( num == 6 ){
weekday = "星期六";
}else if ( num == 7 ){
weekday = "星期日";
}else {
weekday = "你输入的数字不合法";
}
*/ //用switch语句实现:
switch( num ){
case :
weekday = "星期一";
break;
case :
weekday = "星期二";
break;
case :
weekday = "星期三";
break;
case :
weekday = "星期四";
break;
case :
weekday = "星期五";
break;
case :
weekday = "星期六";
break;
case :
weekday = "星期日";
break;
default:
weekday = "你输入的数字不合法";
}
System.out.println( weekday); }
}

4>.case的穿透

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class Quarter{
public static void main(String[] args){
/*
从键盘输入月份数字,显示是第几个季度
*/
Scanner s = new Scanner(System.in);
System.out.print("请输入月份:");
int month = s.nextInt(); switch(month){
case :{}
case :{}
case :{
System.out.println(month + "属于第一季度");
break;
}
case :{}
case :{}
case :{
System.out.println(month + "属于第二季度");
break;
}
case :{}
case :{}
case :{
System.out.println(month + "属于第三季度");
break;
}
case :{}
case :{}
case :{
System.out.println(month + "属于第四季度");
break;
}
default :{
System.out.println("输入的月份有误");
break;
}
} }
}

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>.计算当前的时间

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class CurrentTime{
public static void main(String[] args){
/*
使用System.currentTimeMillis() 计算当前时间;System.currentTimeMillis() 可以
返回当前时间与协调世界时 1970 年 1 月1 日物业之间的时间。
*/ long sumMillis = System.currentTimeMillis(); //把1970.1.1午夜的毫秒数保存到一个变量中 long sumSeconds = sumMillis/1000; //总的秒数 long currentSecond = sumSeconds%60; //当前的秒数 long sumMinutes = sumSeconds / 60; //总的分钟数 long currentMinute = sumMinutes % 60; //当前的分钟数 long sumHours = sumMinutes / 60; //总的小时数 long currentHour = sumHours % 24; //当前的小时数 long beijingHour = (currentHour + 8) % 24; //计算北京的时间,注意,UTC时间和北京时间有时差为8小时。 System.out.println(beijingHour + ":" + currentMinute + ":" + currentSecond); }
}

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

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ import java.util.Scanner; public class PointDistance{
public static void main(String[] args){
/*
从键盘上输入两个点的坐标,计算这两个点的距离,Math.pow(a,b)返回a的b次方
*/
Scanner Input = new Scanner(System.in);
System.out.print("请输入第一个点的坐标:>>>");
int x1 = Input.nextInt();
int y1 = Input.nextInt();
System.out.print("请输入第二个点的坐标:>>>");
int x2 = Input.nextInt();
int y2 = Input.nextInt(); int temp = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); double distance = Math.pow( temp,0.5 ); System.out.println("这两个点的距离为:" + distance ); }
}

3>.