最基本的四则算术运算+-*/,举例如下:
/**
* calculate ((sqrt(20)+sqrt(10))/(sqrt(20)-sqrt(10))
* Math.round只能对小数点后第一位四舍五入,保留整数位;如果想要保留小数点后一位,就先乘以10,四舍五入后再除回来,
* 注意:除以10只保留整数位(非四舍五入直接丢掉小数位),除以10.0则保留小数点后1位
*/
System.out.println("calculate (sqrt(20)+sqrt(10))/(sqrt(20)-sqrt(10)="+
Math.round(10*(Math.sqrt(20)+Math.sqrt(10))/(Math.sqrt(20)-Math.sqrt(10)))/10.0
);
关系运算:
运算种类 | 运算符号 | 运算表达式 |
等于 | == | 表达式1 == 表达式2 |
不等于 | != | 表达式1 != 表达式2 |
大于等于 | >= | 表达式1 >= 表达式2 |
小于等于 | <= | 表达式1 <= 表达式2 |
严格大于 | > | 表达式1 > 表达式2 |
严格小于 | < | 表达式1 < 表达式2 |
运算结果为boolean型,值为true或者false。
关系运算经常用在判断语句和流程控制中,举例如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("put in your math score");
int score = scanner.nextInt();
boolean Pass;
Pass = (score>= 75);
System.out.println("you result of math is Passed:"+Pass);
}
}