zoukankan      html  css  js  c++  java
  • 9.16日上课总结

    动手动脑总结:

    1.1仔细阅读示例: EnumTest.java,运行它,分析运行结果?

    numTest.java

     1 public class EnumTest {
     2 
     3     public static void main(String[] args) {
     4         Size s=Size.SMALL;
     5         Size t=Size.LARGE;
     6         //s和t引用同一个对象?
     7         System.out.println(s==t);  //
     8         //是原始数据类型吗?
     9         System.out.println(s.getClass().isPrimitive());
    10         //从字符串中转换
    11         Size u=Size.valueOf("SMALL");
    12         System.out.println(s==u);  //true
    13         //列出它的所有值
    14         for(Size value:Size.values()){
    15             System.out.println(value);
    16         }
    17     }
    18 
    19 }
    20  enum Size{SMALL,MEDIUM,LARGE};

     运行结果为:

    false
    false
    true
    SMALL
    MEDIUM
    LARGE

    1.2.你能得到什么结论?你掌握了枚举类型的基本用法了吗?

    结论:枚举类型调用的不是同一个对象,枚举类型可以调用,并且调用的不是原始数据(boolean、char、byte、short、int、long、float、double)。

           并且是由字符串转换而成的数据类型。

    2.1 请运行以下代码(TestDouble.java

     1 package ClassTest;
     2 
     3 public class TestDouble {
     4     
     5     public static void main(String args[]) {
     6         System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
     7         System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
     8         System.out.println("4.015 * 100 = " + (4.015 * 100));
     9         System.out.println("123.3 / 100 = " + (123.3 / 100));
    10     }
    11 
    12 }

    运行结果如下:

    0.05 + 0.01 = 0.060000000000000005
    1.0 - 0.42 = 0.5800000000000001
    4.015 * 100 = 401.49999999999994
    123.3 / 100 = 1.2329999999999999

    原因:计算机为二进制。浮点数无法用二进制进行精确表示。CPU中的浮点数一般由指数与尾数组成,但这样表述会失去一定精确度,使运算产生误差。

    2.2使用 BigDecimal 类解决精度问题。

     1 package ClassTest;
     2 
     3 import java.math.BigDecimal;
     4 
     5 public class TestBigDecimal
     6 {
     7     public static void main(String[] args) 
     8     {
     9         BigDecimal f1 = new BigDecimal("0.05");
    10         BigDecimal f2 = BigDecimal.valueOf(0.01);
    11         BigDecimal f3 = new BigDecimal(0.05);
    12         System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
    13         System.out.println("0.05 + 0.01 = " + f1.add(f2));
    14         System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
    15         System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
    16         System.out.println("0.05 / 0.01 = " + f1.divide(f2));
    17         System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
    18         System.out.println("0.05 + 0.01 = " + f3.add(f2));
    19         System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
    20         System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
    21         System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    22     }
    23 }

    运行结果如下:

    下面使用String作为BigDecimal构造器参数的计算结果:
    0.05 + 0.01 = 0.06
    0.05 - 0.01 = 0.04
    0.05 * 0.01 = 0.0005
    0.05 / 0.01 = 5
    下面使用double作为BigDecimal构造器参数的计算结果:
    0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
    0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
    0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
    0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125
     

    使用String类解决了浮点数的精度问题。

     

    3.1 以下代码的输出结果是什么?

                 

    1 int X=100;
    2 
    3               int Y=200;
    4 
    5               System.out.println("X+Y="+X+Y);
    6 
    7               System.out.println(X+Y+"=X+Y");

    X+Y=100200
    300=X+Y

     为什么会有这样的输出结果?

    第一个数字当中X+Y必须添加(),否则在Java中会被判定为连接符,而不是判定为"+"号,如此一来输出的就时X与Y的值,而在下边的输出当中,X+Y在前边,系统默认为"+",不用添加()即可输出X+Y的值。

    4.1 课后作业

    一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

        二柱立马就想到写一个小程序来做这件事。 这个事情可以用很多语言或者工具来实现: Excel, C/C++, C#, VB, Unix Shell, Emacs, Powershell/Vbscript, Javascript, Perl, Python, …

      课堂测试1:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”

      课堂测试2:

    • (1)题目避免重复;
    • (2)可定制(数量/打印方式);

     课堂测试1:

     1 package ClassroomTest;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 
     6  * @author cuixingyu
     7     *四则运算1: 输出30道两位数的加减乘除并避免出现负数
     8  */
     9 public class Math1 {
    10     public static void main(String[] args) {
    11         String s[]= {"+","-","*","/"};
    12         int [] array=new int[30];
    13         int [] array1=new int[30];
    14         for(int i=0;i<30;i++) {
    15             array[i]=(int)( Math.random()*100);  //随机输出100以内的整数
    16             array1[i]=(int)( Math.random()*100);
    17         }
    18         for(int i=0;i<30;i++)
    19         {int c=(int)( Math.random()*4);  //随机输出 + - *  /
    20         if(array[i]>array1[i])   //避免出现复数
    21         { 
    22             System.out.println(i+1+":"+array[i]+s[c]+array1[i]+"="+"      ");   //输出四则运算的题目
    23            
    24         }
    25         if(array1[i]>array[i])
    26             {
    27             System.out.println(i+1+":"+array1[i]+s[c]+array[i]+"="+"      ");
    28             }
    29 }
    30     }
    31 }

    课堂测试2:

     1 import java.util.Random;
     2 import java.util.Scanner;
     3 
     4 public class Math3 {
     5     
     6     //定义各种变量
     7     public static Scanner sc=null;
     8     public static int s1=new Random().nextInt(100);
     9     public static int s2=new Random().nextInt(100);
    10     public static int s3=new Random().nextInt(100);
    11     public static int a[][]=new int[10000][2];
    12     public static int b[]=new int[10000];
    13     
    14     public static void main(String[] args) {
    15         sc=new Scanner(System.in);
    16         System.out.println("请输入题数:");
    17         int n=sc.nextInt();
    18         System.out.println("请输入每行的题数");
    19         int m=sc.nextInt();
    20         
    21         for(int i=1;i<=n;i++) {
    22             //分行
    23             if((i-1)%m==0) {
    24                 System.out.println();
    25             }
    26             
    27         // 题号
    28         System.out.print(i+":");
    29         
    30         //创建随机数
    31         s1=new Random().nextInt(100);
    32         s2=new Random().nextInt(4);
    33         s3=new Random().nextInt(100);
    34         
    35         //检查重复
    36         a[i][0]=s1;
    37         a[i][1]=s3;
    38         b[i]=s2;
    39         for(int j=0;j<i;j++) {
    40             if(a[j][0]==s1&&a[j][1]==s3&&b[j]==s2) {
    41                 s3 = new Random().nextInt(100);
    42                 while(s3==a[i][1]) {   //查重 避免下一次随机数与上一次一样
    43                     s3 = new Random().nextInt(100);
    44                 }
    45                 s3 = new Random().nextInt(100);
    46                 while(s3==a[i][1]) {}
    47             }
    48         }
    49         
    50         
    51         // 加号输出
    52         if(s2==0) {
    53             System.out.print(s1+"+"+s3+"="+"  ");
    54             
    55         }
    56         // 减号输出
    57         if(s2==1) {
    58             if(s1<s3) {
    59                 System.out.print(s3+"-"+s1+"="+"  ");
    60             }else {
    61                 System.out.print(s1+"-"+s3+"="+"  ");
    62             }
    63         }
    64         //乘号输出
    65         if(s2==2) {
    66             //使运算结果在100以内
    67             while(s1*s3>=100) { 
    68                 s1=new Random().nextInt(100);
    69             }
    70             System.out.print(s1+"*"+s3+"="+"  ");
    71         }
    72         //除号输出
    73         if(s2==3) {
    74             while((s3!=0)&&(s1%s3!=0)) {
    75                 s3=new Random().nextInt(100);
    76             }
    77             System.out.print(s1+"/"+s3+"="+"  ");
    78         }
    79     }
    80     }
    81 }

    对于课堂测试二有以下问题没有解决:

    对于除号的分母不为零的情况不能避免,而且在第一步避免重复之后,在第二步判段满足条件时又会更改s3的值,则又有可能重复,对于此处的解决还有没思路。

  • 相关阅读:
    05 drf源码剖析之认证
    04 drf源码剖析之版本
    03 drf源码剖析之视图
    02 drf源码剖析之快速了解drf
    OA之为用户设置角色和为用户设置权限
    EasyUI之datagrid的使用
    C#之反射
    EasyUI之Layout布局和Tabs页签的使用
    Memcached的使用
    Log4Net的简单使用
  • 原文地址:https://www.cnblogs.com/cxy0210/p/11537449.html
Copyright © 2011-2022 走看看