Java_数据类型和运算符

Java数据类型和运算符
1. 标识符:命名规范
标识发放和变量的标识符:第一个单词小写,第二个单子首字母大写: eatFood();
数字不能用作变量的开头;
2. 关键字和保留字:有特定的作用,如public, class
3. 变量: 代表可操作的存储空间:
①,变量的声明: 数据类型+变量名
②,局部变量在使用前必须先声明,赋初值再使用,从属于方法;
③,成员变量:实例变量,从属于对象,会自动赋值: int=0,double=00,bool=false
④,静态变量:从属于类,生命周期从类加载到卸载;
4. 常量:最好是大写;
主要是利用关键字final来定义一个常量,常量一旦被初始化后不能再更改其值。

5. 基本数据类型:
数值型-byte(1个字节),short(2个字节),int(4个字节),long(8个字节),float(4个字节),double(8个字节)
字符型(文本型)-char(2个字节)
布尔型-boolean
长整型常数的声明,要在后面加L: long b=740000000L;
浮点型常量:float和double,带小数的数据在java中称为浮点型,默认类型是double
float类型的数值后面有一个后缀F来区别于double类型
浮点数是不精确的,不能用于比较;如要比较大小,可以用BigDecimal

6. 位运算符: 进行二进制的运算。 

  3>>2, 位移符,意思是除以4;3>>1, 位移符,意思是除以2;

7. 

自动转化类型:
1. 由容量小的转换为容量大的
2. 数据结果可能会超过表示范围,会发生溢出。

引入Scanner键盘输入

import java.util.Scanner;
public class TestScanner{
    public static void main(String[]args){
        Scanner scanner =  new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name= scanner.nextLine();
        System.out.println("请输入你的爱好:");
        String favor = scanner.nextLine();
        System.out.println("请输入你的年龄:");
        int age = scanner.nextIn();
        
        System.out.println(name);
        System.out.println(favor);
        System.out.println("来到地球上的天数:"+age*365)
        
        }
    }

 

 8, if else使用规范

  

public class test2 {
    public static void main(String[]args) {
        int h=(int)(6*Math.random()+1);
        System.out.println(h);
        if(h<3) {
            System.out.println("small");
        }else {
            System.out.println("big");
        }
    }
}

9.

switch 多选择结构:只用于多值判断;
  . switch后面要跟一个表达式;

  

public class test2 {
    public static void main(String[]args) {
        int month = 1;//(int)(1+12*Math.random());
        System.out.println("Month:"+month);
        
        switch(month) {
        case 1:
            System.out.println("123123");
            break;
        case 2: 
            System.out.println("456456");
            break;
        case 3:
            System.out.println("others");
            break;
        }
    }
}

10. 循环嵌套:

//嵌套循环

public class test2 {
    public static void main(String[]args) {
        for(int i=1;i<5;i++);{
            for(int j=1;j<=5;j++) {
                System.out.print(i+"\t"); //"\t"是制表符
            }
            System.out.println(); //ln 是打印后换行
        }
    }
}

  9*9乘法表:

  

public class test2 {
    public static void main(String[]args) {
        for(int n=1;n<=9;n++) {
            for(int m=1;m<=n;m++) {
                System.out.print(m+"*"+n+"="+(m*n)+"\t");
            }
            System.out.println();
        }
    }
}

 

 

 

 

posted on 2018-09-06 08:51  你是不夜星空  阅读(177)  评论(0编辑  收藏  举报

导航