一, 动手动脑 1
1, 问题: 仔细阅读示例: EnumTest.java, 运行它, 分析运行结果? 你能得到什么结论? 你掌握了枚举类型的基本用法了吗?
2, 代码如下:
- public class EnumTest {
- public static void main(String[] args) {
- Size s = Size.SMALL;
- Size t = Size.LARGE;
- // s 和 t 引用同一个对象?
- System.out.println(s == t); //
- // 是原始数据类型吗?
- System.out.println(s.getClass().isPrimitive());
- // 从字符串中转换
- Size u = Size.valueOf("SMALL");
- System.out.println(s == u); // true
- // 列出它的所有值
- for (Size value : Size.values()) {
- System.out.println(value);
- }
- }
- }
- enum Size {
- SMALL, MEDIUM, LARGE
- };
3, 运行结果截图
4, 结论
1枚举变量中实例化的对象的不同元素的地址不同
2从字符串中转换的枚举变量中实例化的对象的的元素赋给新的变量和原变量的地址相同
二, 动手动脑 2
1, 问题:
以下代码的输出结果是什么? 为什么会有这样的输出结果?
- public class Test {
- public static void main(String[] args) {
- int X=100;
- int Y=200;
- System.out.println("X+Y="+X+Y);
- System.out.println(X+Y+"=X+Y");
- }
- }
2, 运行结果截图
3, 结论
在 System.out.println() 输出语句中 "+" 可用来连接字符串, 例如第一行的输出 "X+Y="+X+Y 在等号右边的加号即为连接 X 和 Y 字符串的符号, 所以输出结果为 100200;
加号也可用来进行代数运算, 但要将进行运算的数字之间用括号括起来.
三, 课堂测试 1,2
课堂测试 1: 像二柱子那样, 花二十分钟写一个能自动生成 30 道小学四则运算题目的 "软件"
课堂测试 2: (1) 题目避免重复;
(2) 可定制 (数量 / 打印方式);
代码如下:
- import java.util.Random;
- import java.util.Scanner;
- public class ceishi {
- static Scanner sc = null;
- static String a3 = new String(" ");
- public static int s1 = new Random().nextInt(100);
- public static int s2 = new Random().nextInt(100);
- public static int s3 = new Random().nextInt(100);
- public static int a[][] = new int[100000][2];
- public static int s[] = new int[100000];
- public static void main(String[] args) {
- sc = new Scanner(System.in);
- System.out.println("请输入题目数:");
- int n = sc.nextInt();
- System.out.println("请输入每行的题数:");
- int m = sc.nextInt();
- for (int i = 0; i <n; i++) {
- if ((i) % m == 0)
- System.out.print("\n");
- System.out.print((i + 1) + ":");
- s1 = new Random().nextInt(100);
- System.out.print(s1);
- s2 = new Random().nextInt(100);
- s3 = new Random().nextInt(100);
- // 重复
- a[i][0] = s1;
- a[i][1] = s3;
- s[i] = s2;
- for (int j = 0; j < i; j++) {
- if (a[j][0] == s1 && a[j][1] == s3 && s[j] == s2) {
- s3 = new Random().nextInt(100);
- while (s3 == a[i][1]) {
- s3 = new Random().nextInt(100);
- }
- s3 = new Random().nextInt(100);
- while (s3 == a[i][1]) {
- }
- }
- }
- if (s2 % 4 == 0)
- System.out.print("+");
- if (s2 % 4 == 1) {
- System.out.print("-");
- while (s1 < s3) {
- s3 = new Random().nextInt(100);
- }
- }
- if (s2 % 4 == 2) {
- System.out.print("*");
- while (s1 * s3>= 100) {
- s3 = new Random().nextInt(100);
- }
- }
- if (s2 % 4 == 3) {
- System.out.print("/");
- while (s3 != 0 && s1 / s3 != 0) {
- s3 = new Random().nextInt(100);
- }
- }
- System.out.print(s3);
- System.out.print("=");
- System.out.print(a3);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3201307.html