一、算数运算符
算数运算符即数学中的四则运算。
例题:
public class demo1_8 {
public static void main(String[] args){
//demo1
System.out.println("demo1");
System.out.println((9.5*4.5-2.5*3)/(45.5-3.5));
System.out.println("-----------------------------------------------");
//demo2
System.out.println("demo2");
System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11));
System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11+1.0/13));
System.out.println("-----------------------------------------------");
//demo3
System.out.println("demo3");
double r = 5.5;
double C = 2*r*3.14;
double S = r*r*3.14;
System.out.println("C="+C+",S="+S+"");
System.out.println("-----------------------------------------------");
//demo4
System.out.println("demo4");
double mi=14/1.6;
System.out.println("mi="+mi+"");
double v=mi/(45.5/60);
System.out.println("v="+v+"");
System.out.println("-----------------------------------------------");
//demo5
System.out.println("demo5");
double mi1=24/1.6;
System.out.println("mi1="+mi1+"");
double v1=mi/(1+((40*60+35)/3600));
System.out.println("v1="+v1+"");
System.out.println("-----------------------------------------------");
//demo6
System.out.println("demo6");
double a = 3.4;
double b = 50.2;
double c = 2.1;
double d = 0.55;
double e = 44.5;
double f = 5.9;
System.out.println("x="+(e*d-b*f)/(a*d-b*c)+"");
System.out.println("y="+(a*f-e*c)/(a*d-b*c)+"");
System.out.println("-----------------------------------------------");
//demo7
System.out.println("demo7");
long totalMillisecends = 1203183086328L;
long totalSecends = totalMillisecends/1000;
System.out.println("totalSecends="+totalSecends+"");
long nowSecends = totalSecends%60;
System.out.println("nowSecends="+nowSecends+"");
long totalMinutes = totalSecends/60;
System.out.println("totalMinutes="+totalMinutes+"");
long nowMinutes = totalMinutes%60;
System.out.println("nowMinutes="+nowMinutes+"");
long totalHours = totalMinutes/60;
System.out.println("totalHours="+totalHours+"");
long nowHours = totalHours%24;
System.out.println("nowHours="+nowHours+"");
}
}
public class demo9_15 {
public static void main(String[] args){
//demo9
System.out.println("demo9");
double C = 43;
double F = ((9.0/5)*C)+32.0;
System.out.println(F);
System.out.println("-----------------------------------------------");
//demo10
System.out.println("demo10");
double r = 4.0;
double h = 5.0;
double S = r*r*3.14;
System.out.println("S="+S+"");
double V = S*h;
System.out.println("V="+V+"");
System.out.println("-----------------------------------------------");
//demo11
System.out.println("demo11");
int m = 123;
int n = m%10; //3
int x = m/10; //12
int y = x%10; //2
int z = m/100; //1
System.out.println("sum="+(n+y+z)+"");
//demo13
System.out.println("demo13");
double M = 69.9;
double IT = 5.0;
double FT = 98.0;
double Q = M * (FT - IT) * 4184;
System.out.println("Q="+Q+"");
//demo14
System.out.println("-----------------------------------------------");
System.out.println("demo14");
int deposit = 100;
double M1 = deposit * (1 + 0.00417);
double M2 = (deposit + M1) * (1 + 0.00417);
double M3 = (deposit + M2) * (1 + 0.00417);
double M4 = (deposit + M3) * (1 + 0.00417);
double M5 = (deposit + M4) * (1 + 0.00417);
double M6 = (deposit + M5) * (1 + 0.00417);
System.out.println("M6="+M6+"");
//demo15
System.out.println("-----------------------------------------------");
System.out.println("demo15");
double x1 = 1.5,y1 = 4 , x2 = -3.4 , y2 = 5;
System.out.println("a="+Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))+"");
}
}
二、关系运算符
>
<
>=
<=
==
!=
注意:关系运算符最终结果是一个boolean类型,表示是否成立!!!
true:成立
false:不成立
例题:
public class ExpressionTest{
public static void main(String[] args) {
boolean result1 = false && true || true;
System.out.println(result1);
}
}
三、逻辑运算符
&&(||) 和 &(|)的区别:
&& 和 ||,叫做短路与(或),如果前面的条件已经可以得到结果,则不会继续向后判断,效率较高;
& 和 |,即便根据前面的结果已经得到结果,还是会继续完成所有判断,再返回结果,效率较低。
一般开发使用&& 和 ||。
例题:
package com.simple.Word;
import java.util.Scanner;
//判断一个点是否在一个长方形中
//++(5,2.5)
//-+(-5,2.5)
//--(-5,-2.5)
//+-(5,-2.5)
public class Demo27 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个点:(x,y):");
double x = sc.nextDouble();
double y = sc.nextDouble();
while(x < 0 && y < 0){
if ((-5-x) < 0 && (-2.5 - y < 0)) {
System.out.println("The point is in triangle.");
break;
}else{
System.out.println("The point is not in triangle.");
break;
}
}
while(x > 0 && y > 0){
if ((5 - x) > 0 && (2.5 - y )> 0) {
System.out.println("The point is in triangle.");
break;
}else{
System.out.println("The point is not in triangle.");
break;
}
}
while(x < 0 && y > 0){
if ((-5-x) < 0 && (2.5 - y ) > 0) {
System.out.println("The point is in triangle.");
break;
}else{
System.out.println("The point is not in triangle.");
break;
}
}
while(x > 0 && y < 0){
if ((5-x) > 0 && (-2.5 - y) < 0) {
System.out.println("The point is in triangle.");
break;
}else{
System.out.println("The point is not in triangle.");
break;
}
}
}
}
[点击并拖拽以移动]
四、自增和自减运算符
例题:
public class Test05 {
public static void main(String[] args) {
int a = 10;
int b = a++; // int b = a; a = a + 1;
System.out.println("a = "+ a +", b = "+ b); // a = 11 b = 10
int c = ++a; // a = a + 1; int c = a
System.out.println("a = "+ a +", c = "+ c); // a = 12 c = 12
int d = a--; // a = a - 1
System.out.println("a = "+ a +", d = "+ d); // a = 11 d = 12
int e = --a; // a = a - 1
System.out.println("a = "+ a +", e = "+ e); // a = 10 e = 10
}
}
注意1:对于++、-- 自家自减运算符而言,不管前还是后,对于变量本身而言
肯定要进行加1或者减1操作注意2:在自加运算符中,前加加或者前减减优先级别非常高,仅次于括号, 后加加和后减减的优先级别非常低,甚至比赋值符(=)还低
五、赋值运算符
= // 将右侧的值,最终赋值给左侧
+= // a += 10 <==> a = a + 10
-= // a -= 1 <==> a = a - 1
*= //a * = 1 <==> a = a * 1
/= //a /= 1 <==> a = a / 1
%= //a % = 1 <==> a = a % 1
例题:
public static void main(String[] args)
{
int a1 = 10;
int b1 = 10;
int a2,b2;
a2 = b2 = 10;
int b3 = 10,b4 = 20;
int num1 = 10;
num1 += 1; //num1 = num1 + 1;
System.out.println(num1); //11
int num2 = 24;
num2 %= 1;
System.out.println(num2);
int c = 10;
//c = c + 2;
c += 1;
System.out.println(c);
int d = 10;
//d = d * 2;
d *= 1;
System.out.println(d);
int e= 10;
//e = e * 2;
e *= 1;
System.out.println(e);
}
六、三目(木)运算符
表达式 ? a : b
如:
int c = a < b ? 100: 1000
七、移位运算符
& // 按位与 两者为真,否则为真
| // 按位或 两者为假,否则为假
^ // 异或 相反为真,否则为假
~ // 按位取反 所有位,都进行取反,注意(和反码不一样)
<< // 二进制左移运算
>> // 二进制右移运算
>>> // 无符号二进制右移
num<<1: 表示将num的二进制值向左移动一位,右面补0
num >>1:表示将num的二进制值向右移动一位,从右侧去掉一位
num>>>:无符号右移,忽略符号位,空位都以0补齐
移位运算的一些规则,在运算中的使用
1、&运算符
2、异或运算
例题:
public class Demo {
public static void main(String[] args) {
int a = 23;
System.out.println("原始a二进制:"+getBinaryString(a ));
a = a <<1;
System.out.println("a向左移位:"+getBinaryString(a));
a= a>>1;
System.out.println("a向右移位:"+getBinaryString(a));
a= a>>>1;
System.out.println("a向右移位,忽略符号位:"+getBinaryString(a));
int a2= -1;
System.out.println("原始a2二进制:"+getBinaryString(a2));
a= a2<<1;
System.out.println("a2向左移位:"+getBinaryString(a2));
a2= a2>>1;
System.out.println("a2向右移位:"+getBinaryString(a2));
a2= a2>>>1;
System.out.println("a2向右移位,忽略符号位:"+getBinaryString(a2));
}
/**
* 返回二进制格式字符串
* @param a
* @return
*/
static String getBinaryString(int a){
return Integer.toBinaryString(a);
}
}