Java的算术方法
运算符
1.算术运算符 +,-,,/,%,++,–
2.赋值运算符 =,+=,-=,=,/=,%=
3.关系运算符 ==,!=,<,<=,>,>=
4.逻辑运算符 !,&,&&,|,||
5.条件运算符 ?,:
Package com.imooc.operator;
Public class MathDemo1{
Public static void main (String[]args){
//x++
Int x=4;
Int y=(x++)+5;
System.out.println(“x=”+x+”,y=”+y);
//++x的形式
x=4;
y=(++x)+5;
System.out.println(“x=”+x+”y=”+y);
//x—的形式
X=4;
Y=(x–)+x;
System.out.println(“x=”+x+”y=”+y);
//–x的形式
X=4;
Y=(–x)+x;
System.out.println(“x=”+x+”y=”+y);
Package com.imooc.operator;
Public class MathDemo{
Public static void main(String[]args){
Int num1=10,num2=5;
Int result;//存放结果
//加法
Result=num1=num2;
System.out.println(num1+”+”+num2+”=”+result);
//字符串连接
System.out.println(“”+num1+num2);
//减法
Result=num1-num2;
System.out.println(num1+”-“+num2+”=”+result);
//乘法
Result=num1num2;
System.out.println(num1+””+num2+”=”+result);
//除法
Result=num1/num2;
System.out.println(num1+”/”+num2+”=”+result);
//分子分母都是整型时,结果为整除后的结果
System.out.println(13/5);
System.out.println(“13/5.0”+13.0/5);
//求余数
Result=13%num2;
System.out.println(“13%”+num2+”=”+result);
System.out.println(“13.5%5=”+(13.5%5));
Package com.imooc.operator;
Public class RelateDemo{
Public static void main(String[]args){
Int a=3,b=5;
System.out.println(“a<b=”+(a<b));
System.out.println(“a>b=”+(a.b));
}
}
Pckage com.imooc.operator;
Public class ConditionDemo1{
Public static viod main(String[]args){
//例:商场打折,如果两件商品的价格大于100则减20,并把原价和折后价格分别输出,
//定义两个变量,分别存放两件衣 price服的价格
Double price1+price2;
Price1=80;
Price2=55;
//计算两个商品的总价
Double sum=price1+price2;
//输出原价
System.out.println(“原价为:”+suum);
If(sum>=100){
Sum-=20;//sum=sum-20
}
System.out.println(“折后价格为:”+sum);
}
}
Package com.imooc.operator;
Pulic class ConditionDemo2{
public static void main(String[]args){
//例:判断一个整数是奇数还是偶数,并将价格打印输出,
//定义一个变量存放数据
//int n=11;
//从键盘接受数据
System.out.println(“请输出一个整数:”);
Scanner s=new Scanner(System.in);
Int n=s.nextint();
If(n%2==0)
{
System.out.println(n+”是偶数!”);
}else{
System.out.println(n+”是奇数!”);
}
}
4.3
Package com.imooc.operator;
Import java.until.Scanner;
Public class LogicDemo3{
Public static vod main(String[]args){
//输入一个整数
System.out.println(“请输入一个整数:”);
Scanner sc=new Scanner(System.in);
Int n=sc.nextlnt();
If(!(n%3==0)){
Sysstem,out,println(n+”不能被3整除!”);
}else{
System.out.println(n+”能被3整除!”);
}
}
}
5.1
Package com.imooc.operator;
Public class ConditionDemo{
Public static viod main(String[]args){
Int a=10,b=7;
//求a和b的最大值
Int max;//最大值
If(a>b)
{max=a;}
Else{max=b;}
}
System.out.println(“max=”+max);
max=a>b?a:b;
System.out.println(“max=”+max);
Boolean b1=a>b?(3<6)(true==false);
System.out.println(“b1=”+b1);
}
}
5.3
运算符的优先级
() 圆括号
!,++,-- 逻辑非,自增,自减
,/,% 乘法,除法,取余
+,- 加法,减法
<,<=,>,>= 小于,小于等于,大于大于等于
==,!= 等于,不等于
&& 逻辑与
|| 逻辑或
=,+=,=,%=,-= 赋值运算符,复合赋值运算符
6.1
Package com.imooc.operator;
Import java.uti.Scanner;
Public class LeapYearDemo{
public static viod main(String[]args){
System.out.println(“请输入年份:”);
Scanner sc=new Scanner(System.in);
Int year=sc.nextint();
//闰年的判断规则:能被4整除而不能被100整除的年份,或者能被400整除的年份
If(((year%40)&(year%100!=0))|(year%4000)){
System.out.println(year+“是闰年!”);
}else{
System.out.println(year+”不是闰年!”);
}
}
}