Java中的算术运算与关系运算

最基本的四则算术运算+-*/,举例如下:

/**
 * 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);
		
	}	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值