標籤:sof 常用 and import 數學函數 ... color int 內建函數
主要介紹運算子,和數學函數以及三元運算子:
package testbotoo;public class test1 { public static void main(String[] args) { int a = 122; System.out.println("調用operation的結果是:"+operation(a)); //調用下面定義的方法 };
public static int operation(int v){ int xe = v + 1; int and = 10; return xe ; }}
運算子,基本和python的類似,只是這裡用的是 && 表示 and,用 || 表示 or :
/*關係運算子*/ expression1 && expression2 //相當於python中的 expression1 and expression2 expression1 || expression2 //相當於python中的 expression1 or expression2
三元操作符:
/*三元操作符*/ condition ? expression1 : expression2 //當條件condition為真的時候,計算第一個運算式,否則計算第二個運算式
例如:
x < y ? x : y
//上述就是求 x 和 y 中的的最小的一個
常用數學函數:
就相當於內建函數,已經實現的一些方法,屬於Math類。
Math.sin
Math.cos
Math.exp
Math.log
Math.log10
... ...
使用的時候先匯入:
import static java.lang.Math.*
這樣就省去了加上Math的麻煩。直接使用,sin,cos,tan等等。
java===java基礎學習(2)---運算子,三元操作符,數學函數