JAVA程式語法_數學運算

數學運算

先來個  四則運算 搭配  printf  輸出  暖身



 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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myapp;

import java.util.Scanner;

/**
 *
 * @author chous
 */
public class MyApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in );
        double d1 , d2 ;
        System.out.println("Please input first double num");
        d1 = input.nextDouble();
        System.out.println("Please input second double num");
        d2 = input.nextDouble();
        
        double addResult , subResult , multiResult , divResult;
        addResult = d1 + d2;
        subResult = d1 - d2;
        multiResult = d1 * d2;
        divResult = d1 / d2;
        
        System.out.printf("The result of Adding %f to %f is %f\n"
                            ,d1,d2,addResult);
        System.out.printf("The result of Subtracting %f to %f is %f\n"
                            ,d1,d2,subResult);
        System.out.printf("The result of Multiplying %f to %f is %f\n"
                            ,d1,d2,multiResult);
        System.out.printf("The result of Dividing %f to %f is %f\n"
                            ,d1,d2,divResult);  
        input.close();
    }
}


OK  那其實  還有很多我們可能會需要用到的較為複雜
數學運算

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

Math 程式區域


 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javamath;

import java.util.Scanner;

/**
 *
 * @author chous
 */
public class JAVAMath {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        System.out.println("Please input any decimal number"+
                            "(positive or negative)");
        System.out.println("input num1:");
        double n1 = input.nextDouble();
        System.out.println("input num2:");
        double n2 = input.nextDouble();
        //absolute value
        System.out.printf("The absolute value of %f is: %f\n"
                        ,n1
                        ,Math.abs(n1));
        System.out.printf("The pow (squared) value of %f is: %f\n"
                        ,n1
                        ,Math.pow(n1,2));
        System.out.printf("The pow (cubed) value of %f is: %f\n"
                        ,n1
                        ,Math.pow(n1,3));
        //max
        System.out.printf("The maximum value of %f and %f is: %f\n"
                        ,n1
                        ,n2
                        ,Math.max(n1,n2));
        //min
        System.out.printf("The minimum value of %f and %f is: %f\n"
                        ,n1
                        ,n2
                        ,Math.min(n1,n2));
        //ceiling value -->天花板函數 ceil(x)
        //不小於x的最小整數,如ceil(0.5)=1, ceil(-2.5)=-2,ceil(3.5)=4, ceil(10)=10
        System.out.printf("The ceiling value of %f is: %f\n"
                        ,n1
                        ,Math.ceil(n1));
        //floor value -->地板函數 floor(x)又稱「高斯函數」(向下取整/向下舍入)
        //不大於x的最大整數,如 [0.9]=0, [5.1]=5,[-1.5]=-2, [19]=19 ,[4.99]=4,[-3.4]=-4
        System.out.printf("The floor value of %f is: %f\n"
                        ,n1
                        ,Math.floor(n1));
        
        //log (base 2)
        System.out.printf("The Log base 2 value of %f is: %f\n"
                        ,n1
                        ,Math.log(n1));
        //log (base 10)
        System.out.printf("The Log base 10 value of %f is: %f\n"
                        ,n1
                        ,Math.log10(n1));
        System.out.printf("The round value of %f is: %d\n"
                        ,n1
                        ,Math.round(n1));

        //hypot value -->  三角形斜邊
        double sideX , sideY;
        System.out.println("Input the x-side values of Triangle:");
        sideX = input.nextDouble();
        System.out.println("Input the y-side values of Triangle:");
        sideY = input.nextDouble();
        System.out.printf("The hypotenuse value of %f and %f is: %f\n"
                        ,sideX
                        ,sideY
                        ,Math.hypot(sideX,sideY));
    }   
}





留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題