9.15课堂验证(已在文件发表)

TestDouble:

源代码:

public class TestDouble {

    public static void main(String args[]) {
        System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
        System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
        System.out.println("4.015 * 100 = " + (4.015 * 100));
        System.out.println("123.3 / 100 = " + (123.3 / 100));
    }
}

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=12542:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 TestDouble

0.05 + 0.01 = 0.060000000000000005

1.0 - 0.42 = 0.5800000000000001

4.015 * 100 = 401.49999999999994

123.3 / 100 = 1.2329999999999999

 

Process finished with exit code 0

 

 

 

TestBigDecimal:

源代码:


import java.math.BigDecimal;

public class TestBigDecimal
{
    public static void main(String[] args)
    {
        BigDecimal f1 = new BigDecimal("0.05");
        BigDecimal f2 = BigDecimal.valueOf(0.01);
        BigDecimal f3 = new BigDecimal(0.05);
        System.out.println("����ʹ��String��ΪBigDecimal�����������ļ�������");
        System.out.println("0.05 + 0.01 = " + f1.add(f2));
        System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f1.divide(f2));
        System.out.println("����ʹ��double��ΪBigDecimal�����������ļ�������");
        System.out.println("0.05 + 0.01 = " + f3.add(f2));
        System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
}

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=12762:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 TestBigDecimal

����ʹ��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

 

Process finished with exit code 0

 

 

 

Test:

源代码:

public class Test {
    public static void main(String[] args) {
        int intValue=100;
        long longValue=intValue;
        double doubleValue=1234567890;
        float floatValue=(float)doubleValue;
        System.out.println(floatValue);//1.23456794E9

        int X=100;
        int Y=200;
        System.out.println("X+Y="+X+Y);
        System.out.println(X+Y+"=X+Y");
        doNotRunme();

        String string="";
        double d1=1000.123;
        double d2=1000.123;
        if(Math.abs(d2-d1)<1e-10){

        }
        //System.out.println(string);

    }

    public static void doNotRunme()
    {
        doNotRunme();
    }
}

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=14816:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 Test

1.23456794E9

X+Y=100200

300=X+Y

Exception in thread "main" java.lang.StackOverflowError

       at Test.doNotRunme(Test.java:27)

       at Test.doNotRunme(Test.java:27)

       at Test.doNotRunme(Test.java:27)

       at Test.doNotRunme(Test.java:27)

       at Test.doNotRunme(Test.java:27)

RandomStr:

源代码:

public class RandomStr
{
    public static void main(String[] args)
    {
        //����һ�����ַ���
        String result = "";
        //����6��ѭ��
        for(int i = 0 ; i < 6 ; i ++)
        {
            //����һ��97��122��int�͵�����
            int intVal = (int)(Math.random() * 26 + 97);
            //��intValueǿ��ת��Ϊchar�����ӵ�result����
            result = result + (char)intVal;
        }
        //�������ַ���
        System.out.println(result);
    }
}

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=1482:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 RandomStr

hblntk

 

Process finished with exit code 0

 

 

 

 

InputTest:

源代码:

/**
 @version 1.10 2004-02-10
 @author Cay Horstmann
 */

import java.util.*;

public class InputTest
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        // get first input
        System.out.print("What is your name? ");
        String name = in.nextLine();

        // get second input
        System.out.print("How old are you? ");
        int age = in.nextInt();


    /* int i;
     String value="100";
     i=Integer.parseInt(value);
     i=200;
     String s=String.valueOf(i);*/

        // display output on console
        System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));


    }
}

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=1659:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 InputTest

What is your name? zhengdun

How old are you? 19

Hello, zhengdun. Next year, you'll be 20

 

Process finished with exit code 0

 

 

 

 

 

EnumTest:

源代码:


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};

 

运行结果:

"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\lib\idea_rt.jar=7281:C:\Program Files\JetBrains\IntelliJ IDEA 2023.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\郑盾\IdeaProjects\test9_15\out\production\test9_15 EnumTest

false

false

true

SMALL

MEDIUM

LARGE

 

Process finished with exit code 0

 

 

 

 

Additon:

源代码:


// An addition program

import javax.swing.JOptionPane;  // import class JOptionPane

public class Addition {
    public static void main( String args[] )
    {
        String firstNumber,   // first string entered by user
                secondNumber;  // second string entered by user
        int number1,          // first number to add
                number2,          // second number to add
                sum;              // sum of number1 and number2

        // read in first number from user as a string
        firstNumber =
                JOptionPane.showInputDialog( "Enter first integer" );

        // read in second number from user as a string
        secondNumber =
                JOptionPane.showInputDialog( "Enter second integer" );

        // convert numbers from type String to type int
        number1 = Integer.parseInt( firstNumber );
        number2 = Integer.parseInt( secondNumber );

        // add the numbers
        sum = number1 + number2;

        // display the results
        JOptionPane.showMessageDialog(
                null, "The sum is " + sum, "Results",
                JOptionPane.PLAIN_MESSAGE );

        System.exit( 0 );   // terminate the program
    }
}

 

运行结果:

 

 

posted @ 2023-09-17 21:10  KuanDong24  阅读(28)  评论(0编辑  收藏  举报