zoukankan      html  css  js  c++  java
  • Java动手动脑及课后实验

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

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

    测试截图:

    结论:枚举类型在Java中的应用较为简单,其中java.lang.Class.isPrimative()确定指定的Class对象表示一个基本类型。

    下面是关于java.lang.Class.isPrimative()方法的声明:

    public boolean isPrimitive();

    当该类表示一个基本类型时,该方法的返回值是true,否则返回false.

    关于枚举基本掌握了。

     

    2.两数相加

    源文件:Addition.java 使用两个输入框输入数据 用一个消息框显示计算结果

      

     1 //An addition program 
     2 
     3 import javax.swing.JOptionPane;  // import class JOptionPane
     4 
     5 public class Addition {
     6 public static void main( String args[] )
     7 {
     8    String firstNumber,   // first string entered by user
     9           secondNumber;  // second string entered by user
    10    int number1,          // first number to add
    11        number2,          // second number to add
    12        sum;              // sum of number1 and number2
    13 
    14    // read in first number from user as a string
    15    firstNumber =
    16       JOptionPane.showInputDialog( "Enter first integer" );
    17 
    18    // read in second number from user as a string
    19    secondNumber =
    20       JOptionPane.showInputDialog( "Enter second integer" );
    21 
    22    // convert numbers from type String to type int
    23    number1 = Integer.parseInt( firstNumber ); 
    24    number2 = Integer.parseInt( secondNumber );
    25 
    26    // add the numbers
    27    sum = number1 + number2;
    28 
    29    // display the results
    30    JOptionPane.showMessageDialog(
    31       null, "The sum is " + sum, "Results",
    32       JOptionPane.PLAIN_MESSAGE );
    33 
    34    System.exit( 0 );   // terminate the program
    35 }
    36 }

    收获:

    3.请运行以下代码(TestDouble.java)

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

    测试截图:

    收获:

     4.解决方法——使用BigDecimal类

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

    测试截图:

    收获:

  • 相关阅读:
    pikachu——Unsafe Filedownload Fileupload(不安全的文件下载和上传)
    pikachu——Files Inclusion(文件包含漏洞)
    pikachu——RCE
    pikachu——SQL注入
    pikachu——XSS(跨网站脚本漏洞)
    pikachu——CSRF
    pikachu——暴力破解
    GCD 信号量 dispatch_semaphore_t
    【转】iOS 9 Storyboard 教程(一上)
    一个大小为N的数组,里面是N个整数,怎样去除重复的数
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/13755984.html
Copyright © 2011-2022 走看看