hooyantsing's Blog

第3次课程

字数统计: 1.4k阅读时长: 7 min
2019/04/10

源辰74班

第3次课程

04.13.2019

导图

思维导图.png

内容

1.数据类型:基本型

|类型 |占用空间|范围 |
|boolean|1bit |true/false |
|byte |1Byte |-128~127 |
|short |2Byte |-32768~32767 |
|char |2Byte |0~65545 |
|int |4Byte |-2147483648~2147483647|
|long |8Byte |-263 ~ 263-1 |
|float |4Byte | |
|double |8byte | |

实验题1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Test0413;
public class Test1 {
    public static void main(String[] args) {
       int ia = 0x55;
       int ib = 011;
       //byte占2个字节,8bit。强制转换后将保留原数值(如:int型)的二进制的后8bit赋值给新byte型。
       /*
        *  例:
        *  十六进制: 0x771 -> 十进制:771
           int二进制时:011101110001
           byte二进制时:   01110001
           得:                十进制:113
       */
       byte ba = (byte)0x771;
       long la = 123456789;
       long lb = 12345678987654321L;
       System.out.println(ia+"\t"+ib+"\t"+ba+"\t"+la+"\t"+lb);
    }
}

实验题2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package Test0413;
public class Test2 {
    public static void main(String[] args) {
       //符号f与F,d与D,e与E不区分大小写,效果一样
       //浮点型默认是double打印。
       float fa = 123.4f;
       //fb等价于fc。
       float fb = 12.5E3F;
       float fc = (float)12.5E3;
       
       double da = 123D;
       double db = 123.456D;
       double dc = 123.45e301;
       
       System.out.println(fa+" "+fb+"  "+fc+" "+da+" "+db+" "+dc);
    }
}

2.数据类型:引用型

|class(类) |
|interface(接口)|
|数组 |

数组实验:

1
2
//创建数组的方式
int[] x = new int[4];
1
2
3
4
5
6
7
8
9
10
11
12
package Test0413;
public class Test4 {
    public static void main(String[] args) {
       //定义一个数组x,系统默认赋值为0(安全)
       int[] x = new int[4];
       
       //打印数组中每一个元素
       for(int i=0;i<4;i++) {
       System.out.println(x[i]);
       }
    }
}

运算优先级实验:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package Test0413;
public class Test4 {
    public static void main(String[] args) {
       // + 运算数为 数字型 则为 加法运算
       // + 运算数为 字符串 则为 联接
       System.out.println("book"+3+4+"price");
       System.out.println(3+4+"book"+"price");
       System.out.println("book"+"price"+3+4);
    }
}

结果:
book34price
7bookprice
bookprice34

3.作用域

实验:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package Test0413;
public class Test6 {
    static int x;
    int y;
    
    public static void main(String[] args) {
       int zz = 90;
       System.out.println(x);
       //System.out.println(y);  //在"静态区"中只能访问静态成员
       
       {//用{}创建一个作用域
           int z = 10;
           System.out.println(z);
           System.out.println(zz);
           //System.out.println(yy);
       }
       //int yy = 70;    //系统从上向下读取,在调用yy之前应该声明yy
       //System.out.println(z);  //在一个作用域定义的变量,在这个作用域外无法访问
    }
}

4.Scanner类:

作用:输入

Scanner实验:

1
2
3
4
5
6
//调用Scanner的方式
Scanner sc = new Scanner(System.in);
//输入数字
String s1 = sc.nextInt();
//输入字符
String s2 = sc.nextLine();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package Test0413;
import java.util.Scanner;
public class Test7 {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入姓名");
       String s = sc.nextLine();
       
       System.out.println("请输入年龄");
       
       int age = sc.nextInt();
       //输入年龄数值后,回车符会被下一行吸收,导致下一行未输入就跳过
       /*
        * 解决方案一:
        * int age = sc.nextInt();
        * sc.nextLine(); \\将多余的回车符用nextLine吸收
        * 解决方案二:
        * int age =  Integer.parseInt(sc.nextLine);  //输入的部分,最后强制转换为数字赋值给age
        *
       */
       System.out.println("请输入性别");
       String sex = sc.nextLine();
       
       System.out.println("姓名为:"+s+"年龄为:"+age+"性别为:"+sex);
    }
}

5.Rammod类:

**** 作用:生成随机数

Rammod实验:

1
2
3
调用Rammod的方式
Random r = new Random();
int x1 = r.nextInt();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Test0413;
import java.util.Random;
public class Test9 {
    public static void main(String[] args) {
       Random r = new Random();
       //随机范围为:0~2147483647
       int x1 = r.nextInt();
       System.out.println(x1);
       //随机范围为:0~9
       int x2 = r.nextInt(10);
       System.out.println(x2);
       //随机范围为:5~9
       int x3 = r.nextInt(5)+5;
       System.out.println(x3);
    }
}

6.[项目]扑克牌发牌系统

****我的算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Test0413;
import java.util.Random;
public class Test_pukepai {
    public static void main(String[] args) {
       Random r = new Random();
       String[] puke = {"方片A","方片2","方片3","方片4","方片5","方片6","方片7","方片8","方片9","方片10","方片J","方片Q","方片K",
                      "梅花A","梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10","梅花J","梅花Q","梅花K",
                      "红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9","红桃10","红桃J","红桃Q","红桃K",
                      "黑桃A","黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",
       };
       for(int i=1;i<14;i++) {
           int a = r.nextInt(52);
           System.out.println(puke[a]);
       }
    }
}

**    老师算法:**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Test0413;

import java.util.Random;
import java.util.Scanner;

public class puke_teacher {

    public static void main(String[] args) {
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入牌的套数");
        int a = sc.nextInt();
        
        int total = 52*a;
        int index = r.nextInt(total);
        String card = genFlower(index);
        card = card + genCardNum(index);
        System.out.println(index+"对应的牌为:"+card);

    }
    //获取花色
    public static String genFlower(int index) {
        String flower = "";
        int flowerindex = index/13%4;
        switch(flowerindex) {
        case 0:flower = "方片";break;
        case 1:flower = "梅花";break;
        case 2:flower = "红桃";break;
        case 3:flower = "黑桃";break;
        }
        return flower;
    }
    //获取牌数值
    public static String genCardNum(int index) {
        String cardNum = "";
        int card = index%13+1;
        cardNum=card+"";
        switch(card) {
        case 1:cardNum = "A";break;
        case 11:cardNum = "J";break;
        case 12:cardNum = "Q";break;
        case 13:cardNum = "K";break;
        }
        return cardNum;
    }
}

其他

****作业附件:

Java笔试题:Java基础概念_学生用.doc

作业题-学生用.doc

CATALOG