二、数据类型补充及分支,循环语句

(一)标识符

  1. 标识符概述:就是给类、方法、变量等起名字的符号
    • 标识符只有数字(09)、字母(AZ和a~z)、美元符号($)、下划线(_)、以及Unicode字符集中符号大于0xC0的所有符号组成构成(各符号之间没有空格);
    • 标识符的第一个符号为字母、下划线和美元符号,后面可以是任何字母、数字、美元符号和下划线;
    • java区分大小写,因此student和Student是两个不同的标识符;
    • 标识符命名,切记不能以数字开头,也不能使用任何java关键字作为标识符,而且不能赋予标识符任何标准的方法名。
  2. 命名规范
    • 小驼峰命名法(方法、变量),一般由多个单词组成时,第一个单词首字母小写,其他首字母单词大写,例如:xxxYyyZzz;
    • 大驼峰命名法(类、接口),一般由多个单词组成时,第一个单词首字母大写,其他单词首字母大写,例如:XxxYyyZzz;
    • 包名:多单词组成时所有字每都小写:xxxyyyzzz

(二)类型转换变换

  1. 自动转换类型:把一个数据范围小的数值或变量赋值给另一个表示范围大的变量。

  2. 强制转换类型:把一个数据范围大的数值或变量赋值给另一个表示范围小的变量。

(三)分支语句

  1. 顺序结构:if语句

    if(关系表达式){
    	语句1;
    }else if(关系表达式){
    	语句2;
    }else if(关系表达式){
    	语句3;
    }else if(关系表达式){
    	语句 n ;
    }else{
    	语句n + 1;
    }
    例题:考试分数的阶段
    public class Studentgrade {
        public static void main(String[] args) {
            Scanner Score = new Scanner(System.in);
            System.out.println("请输入分数:");
            int x = Score.nextInt();
            if (x > 100 || x < 0){
               System.out.println("您输入的分数错误!");
            }else if(x > 89&&x <= 100){
               System.out.println("优秀");
            }else if(x > 79&&x <= 89) {
               System.out.println("良好");
            }else if(x > 59&&x <= 79) {
               System.out.println("及格");
            }else{
               System.out.println("不及格");
            }
        }
    }
    
    

    switch语句:

    ’注意‘:在switch语句中,如果case控制语句体后面不写break,将出现后面全输出现象,直到遇到break或者循环体结束。

    switch(){
    case 1:
    case 2:
    case 3:
    case n:
    default:
    }
    例题:春夏秋冬季节判断
    public class Season {
    public static void main(String[] args) {     
    		 //键盘录入月份数据,使用变量接收	 		
    		 Scanner sc = new Scanner(System.in);		 
    		 System.out.println("请输入一个月份:");		 
    		 int month = sc.nextInt();
    		 //case 穿透
    		 switch(month) {
    		 case 1:
    		 case 2:
    		 case 12:
    			 System.out.println("冬季");
    			 break;
    		 case 3:
    		 case 4:
    		 case 5:
    			 System.out.println("春季");
    			 break;
    		 case 6:
    		 case 7:
    		 case 8:
    			 System.out.println("夏季");
    			 break;
    		 case 9:
    		 case 10:
    		 case 11:
    			 System.out.println("秋季");
    			 break;
    		 default:
    			 System.out.println("您输入的月份有误!");
    			 break;	
    		 }
    	}
    }
    

(四)循环语句

for循环语句

//for循环语句
for(初始化语句;条件判断语句;条件控制语句){
    循环语句体;
}
//执行完后立马判断

典例:判断是否为水仙花数

public class 水仙花数 {
    public static void main(String[] args) {
        Scanner num = new Scanner(System.in);
        System.out.println("请输入一个三位数:");
        int a = num.nextInt();
        int ge = a % 10;
        int shi = a / 10 % 10;
        int bai = a / 10 / 10 %  10;
        int num1 = ge*ge*ge + shi*shi*shi + bai*bai*bai;
        if (a == num1){
            System.out.println("true");
        }else{
            System.out.println("flase");
        }
    }
}

while结构:不知道循环几次时用这个。

while(条件判断语句){
	循环语句体;
	条件控制语句;
}

例题:判断

public class sum100 {
    public static void main(String[] args) {
        int i = 1,sum = 0;
        while (i <= 100){
            sum += i;
            i++;
        }
        System.out.println("sum = "+sum);
    }
}

do{}while :先上车后买买票

do{
	循环语句体;
	条件语句体;
}while(条件判断语句);
  public static void main(String[] args) {
        int i = 1,sum = 0;
        do {
            sum += i;
            i++;
        }while (i <= 100);
        System.out.println("sum = "+sum);
    }

三种循环的区别

  • for循环和while循环先判断后循环,do{}while()循环先执行后判断;
  • 条件语句中的自增变量归属于for的语法结构中,在其结束后不能被使用,但是while语句中的自增变量可以使用。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值