Java学习---成员字段

成员字段

1.定义成员字段

   数据类型 成员字段名;

   int num;

2.Java的基本数据类型

   a. 整形 byte short int long

   b. 浮点型 float double

   c. 字符型 char string

   d. 布尔型 boolean

3.ASCII码转换

使用short 关键字进行转换

publicclass ASCII {

 

    publicstaticvoid main(String[] args){

       char s1 = '@';

       char s2 = '%';

       char s3 = '/';

       char s4 = '^';

       System.out.println(s1+"字符ASCII码:"+(short)s1);

       System.out.println(s2+"字符ASCII码:"+(short)s2);

       System.out.println(s3+"字符ASCII码:"+(short)s3);

       System.out.println(s4+"字符ASCII码:"+(short)s4);

       for (int i = -128; i <127; i++) {

           System.out.print((char)i+"\t");

           if (i%7==0) {

              System.out.println();

           }

       }

    }

}

4.字符加密

import java.util.Scanner;//导入控制台扫描类

publicclass CharA {

    static String str="password";

    publicstaticvoid main(String[] args){

       jiami();

    }

    privatestaticvoid jiami() {

       System.out.println("请输入要加密的字符:");

       Scannerinput = new Scanner(System.in);

       str=input.nextLine();

       for (int i = 0; i <str.length(); i++) {

           char d = (char)(str.charAt(i)+5);

           System.out.print("加密后的字符是:"+d);

       }

       input.close();

    }

5.类型转换

      //浮点数强转为整数

       float a =34.5f;

      System.out.println((int)a);

      //赋值

      int i =100;

       i= 10*10;i=50+50;

       System.out.println(i);

      h =(float)a/b;//强制数据类型转换

6.最大值和最小值

       //short-->Short      

       System.out.println("短整数 最大值:"+Short.MAX_VALUE);

       System.out.println("短整数 最小值:"+Short.MIN_VALUE);

       //int-->Integer

       System.out.println("整数 最大值:"+Integer.MAX_VALUE);

       System.out.println("整数 最小值:"+Integer.MIN_VALUE);

       //long-->Long

       System.out.println("长整数 最大值:"+Long.MAX_VALUE);

       System.out.println("长整数 最小值:"+Long.MIN_VALUE);

       //float-->Float

       System.out.println("单精度浮点数 最大值:"+Float.MAX_VALUE);

       System.out.println("单精度浮点数 最小值:"+Float.MIN_VALUE);

       //double-->Double

       System.out.println("双精度浮点数 最大值:"+Double.MAX_VALUE);

       System.out.println("双精度浮点数 最小值:"+Double.MIN_VALUE);

       int num = 3;//用基本数据类型声明的变量 只做存储

       Integernum1 = 30;//用基本数据类型包装类声明的变量 有属性和方法

       System.out.println(num1);

       //byte--> Byte

       //short-->Short

       //int-->Integer

       //long-->Long

       //float-->Float

       //double-->Double

       //char--> Character

       //boolean--> Boolean

5.格式化输出

       System.out.print("不换行输出");

       System.out.println("换行输出");

       System.out.printf("格式化输出%d",num);

       //声明扫描类对象

       Scannersc = new Scanner(System.in);

       //给一个输入提示

       System.out.println("\n请输入姓名:");

       //扫描下一行 将结果存储在一个字符串变量中

       Stringname = sc.nextLine();

       System.out.println("你的姓名是:"+name);

       System.out.println("请输入你的年龄:");

       int age = sc.nextInt();

       System.out.println("你的年龄是:"+age);

       sc.close();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章