[Java] 02--Variable and data type Scanner

1. What is a constant and what is a variable

You just need to remember two things:

Constant: It is a kind of data that will not change according to the environment, for example: pi.
Variable: It is a kind of data that can generate different values ​​according to different environments, such as: mobile phone power.

In Java, we describe variables through three elements: variable type, variable name, and variable value.
What are the variable types in Java? Variable types in JAVA can be roughly divided into two types: one is a numerical type, the other is a non-numeric type, and the numerical type is divided into two types: integer and non-integer. As shown in the figure: Next, we will start to define variables
insert image description here
. Four steps are required.

  • Determine variable type
  • take variable name
  • assign initial value to variable
  • semicolon ending
  • It consists of letters, underscores, dollar signs ($), numbers, but the first character cannot be a number.

    If the variable name is compound, then we use camel case, or snake case. Camel case: teaCup (meaning teacup), stuAge (student age) Snake form: tea_cup, stu_age
    can be all lowercase if it is a single word.
    Do not use Chinese pinyin to express: For example, the student's age: xueShengNianling.
    Variable names cannot be repeated in a method.

    Variables must give themselves a definite type.

    2. Data type

    There are eight basic types of Java, and the basic types can be divided into three categories.

  • character type char
  • Boolean type boolean
  • Numerical type byte, short, int, long, float, double
  • Numerical types can be divided into integer types byte, short, int, long and floating point types float, double.

    There are no unsigned numeric types in JAVA, and their value range is fixed and will not change with changes in the machine hardware environment or operating system. In fact, there is another basic type void in JAVA, which also has a corresponding wrapper class java.lang.Void, but we cannot directly operate on them. The 8 types represent ranges as follows:

    1. byte: 8 bits, the maximum storage data volume is 255, and the stored data range is between -128~127.
    2. short: 16 bits, the maximum data storage capacity is 65536, and the data range is between -32768 ~ 32767.
    3. int: 32 bits, the maximum data storage capacity is 2 to the 32nd power minus 1, and the data range is from negative 2 to the 31st power to positive 2 to the 31st power minus 1.
    4. long: 64 bits, the maximum data storage capacity is 2 to the 64th power minus 1, and the data range is from negative 2 to the positive 2 to the 63rd power minus 1.
    5. float: 32 bits, the data range is 3.4e-45 ~ 1.4e38, and f or F must be added after the number when directly assigning a value.
    6. double: 64 bits, the data range is 4.9e-324 ~ 1.8e308, and d or D can be added or not added when assigning a value.
    7. boolean: only true and false two values.
    8. char: 16 bits, store Unicode code, assign value with single quotes.

    insert image description here
    insert image description here

    2.1 What is data type conversion

    As the name suggests, data type conversion refers to the mutual conversion between different data types.
    You only need to master two methods for data type conversion:

    1. Automatic type conversion
    2. Mandatory type conversion

    Automatic type conversion
    You only need to remember one sentence about automatic type conversion, which is:A small range of tables can be automatically converted to a large range of tables.
    You can understand it like this: When the water in a small container is changed to a large container, there is no problem, but if the water in a large container is changed to a small container, it may not fit and it will overflow.
    As shown in the figure below:
    insert image description here
    Mandatory type conversion
    You will definitely have doubts, since automatic type conversion can only be converted from a small range of tables to a large range of tables, how do I want to convert from a large range of expressions to a small range in actual use? What to do? Hey, mandatory type conversion is used here. As the name suggests, mandatory type conversion is to convert: a number with a large range of tables to a number with a small range of tables.
    So how to use mandatory type conversion?

    double b = 10.2;
    int a = (int)b
    

    3. Why use Scanner

    The exercises in all of our previous chapters have always been to output a piece of data. We know that a software needs both output and input. The Scanner is used for the user to input data in the Java program .
    So for Scanner, you can understand that it is a scanner used to obtain the data we input from the keyboard.
    how to use?
    You just need to remember three steps:

    1. Before the declaration of the class, introduce the scanner (Scanner): import java.util.Scanner;
    2. Create a scanner in the method Scanner input = new Scanner(System.in);
    3. Obtain from the keyboard through the scanner Input data int i = input.nextInt();

    insert image description here

    package chapter2.step7;
    /********* Begin *********/
    import java.util.Scanner; //1.导入Scanner
    
    
    public class HelloWorld{
          
          
    	public static void main(String[] args){
          
          
            Scanner input = new Scanner(System.in);//2.声明扫描仪
            int age = input.nextInt();//3.获取键盘输入的整数数据
            System.out.println(age);//输出
    		
    		/********* End *********/
    	}
    }
    

    Tip: Scanner cannot obtain the char (character) type, and to obtain the decimal type, you need to use the nextDouble method or the nextFloat method

    //获取输入的小数
    double d = input.nextDouble();//获取输入的双精度类型数据
    float f = input.nextFloat();//获取输入的单精度类型数据
    
    package chapter2.step7;
    /********* Begin *********/
    import java.util.Scanner; //1.导入Scanner
    
    
    public class HelloWorld{
          
          
    	public static void main(String[] args){
          
          
            System.out.println("请录入嫦娥个人信息:");
            Scanner input = new Scanner(System.in);//2.声明扫描仪
             System.out.println("请输入姓名:");
            String name = input.next();
    
            System.out.println("请输入年龄:");
            int age = input.nextInt();//3.获取键盘输入的整数数据
           
            System.out.println("请输入性别:");
            String sex = input.next();
            System.out.println("请输入体重:");
            float weight = input.nextFloat();
            System.out.println("请输入地址:");
            String address=input.next();
            System.out.println("请输入是否已婚:");
            String flag = input.next();
            System.out.println("信息如下:");
            System.out.println('\t'+"姓名:"+name);
            System.out.println('\t'+"年龄:"+age+"岁");
            System.out.println('\t'+"性别:"+sex);
            System.out.println('\t'+"体重:"+weight+"kg");
            System.out.println('\t'+"地址:"+address);
            System.out.println('\t'+"婚否:"+flag);
    		
    		/********* End *********/
    	}
    }
    

    ATM Contents

    package step4;
    
    import java.util.Scanner;
    
    public class ForPractice4 {
          
          
    	public static void main(String[] args) {
          
          
    		/*****start*****/
            Scanner input = new Scanner(System.in);
            System.out.println("欢迎使用中国人民银行ATM取款机");
           
    
    
            int money=1000;
            int flag=2;
            while(flag==2){
          
          
                // 进入取款界面
                System.out.println("输入取款金额:");
                int score = input.nextInt();
                if(score<=money){
          
          
                    money-=score;
                   System.out.print("剩余金额:"+money+",是否继续('1':结束,'2':继续):");
                   flag=input.nextInt();
                   System.out.print("\n");
                }else{
          
          
                    System.out.println("目前余额:"+money+"无法满足您的取款需求!");
                    continue;
                }
                
            }
            System.out.println("取款结束!");
    
    
    		
    		
    		
    		
    		
    		/*****end*****/
    	}
    }
    

    ratio of three numbers

    package step4;
    
     
    public class LianXi_Sort {
          
          
    
    	
    	public static void main(String[] args) {
          
          
    		// TODO Auto-generated method stub
    		/*
    		 * 定义三个整数x,y,z,将这三个整数由小到大排序并输出。
    		 * 例如定义 int x = 9; int y = 10; int z = 8;   -- > x的值改成8  y的值改成9 z的值改成10
    		 * 
    		 * 实现思路:通过if语句对x y z 的值进行匹配,比如x大于y则x和y进行数据交换
    		 * */
    		 java.util.Scanner sc = new java.util.Scanner(System.in);
    		//获取平台分配的x的值
    		int x = sc.nextInt();
    		//获取平台分配的y的值
    		int y = sc.nextInt();;
    		//获取平台分配的z的值
    		int z = sc.nextInt();;
    
    		/**********begin**********/
            sc.close();            //使用完键盘输入后关闭
            int temp;
    
            if(x>y) {
          
                         //如果x>y那么x,y需要交换位置
    
                temp=x;          //借助temp变量,将二者间的大数x存在temp中
    
                x=y;                //让小数y到x的位置
    
                y=temp;   //让大数x(即此时的temp)到y的位置,这时x y中的数就是从小到大的顺序
    
            }
    
            if(x>z) {
          
                      //如果x>z那么x,z需要交换位置
    
                temp=x;  //借助temp变量,将二者间的大数x存在temp中
    
                x=z;        //让小数z到x的位置
    
                z=temp;    //让大数x(即此时的temp)到z的位置,这时x z中的数就是从小到大的顺序
    
            }
    
            if(y>z) {
          
                          //如果y>z那么y,z需要交换位置
    
                temp=y;    //借助temp变量,将二者间的大数y存在temp中
    
                y=z;         //让小数z到y的位置
    
                z=temp;  //让大数y(即此时的temp)到z的位置,这时y z中的数就是从小到大的顺序
    
            }
    
          
    
           
    
    	   
    
    
    	    /**********end**********/
    		System.out.print("x:"+x+" y:"+y+" z:"+z);
    		
    	}
    
    }
    
    

Guess you like

Origin blog.csdn.net/liqiannan8023/article/details/130200649