public class ArithmaticOp{
public static void main(String args[]){
int a=5+4;//a=9
int b=a*2;//b=18
int c=b/4;//c=4
int d=b-c;//d=14
int e=-d;//e=-14
int f=e%4;//f=-2
double g=18.4;
double h=g%4;//h=2.4
int i=3;
int j=i++;//i=4,j=3
int k=++i;//i=5,k=5
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
System.out.println("e="+e);
System.out.println("f="+f);
System.out.println("g="+g);
System.out.println("h="+h);
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("k="+k);
}
}
public class CompareNumber
{
public static void main(String[] args)
{
int n=3;
int m=4;
System.out.println();
System.out.println("n<m is "+(n<m));
System.out.println("n=m is "+((++n)==m));
System.out.println("n>m is "+((++n)>m));
System.out.println("n is "+n);
public class RelationAndConditionOp{
public static void main(String[] args){
int a=25,b=3;
boolean d=a<b;//d=false
System.out.println("a<b="+d);
int e=3;
if(e!=0&&a/e>5)
System.out.println("a/e="+a/e);
int f=0;
if(f!=0&&a/f>5)
System.out.println("a/f="+a/f);
else
System.out.println("f="+f);
}
}
public class LValue{
public static void main(String[] args){
int x=1,y=12;
x+=2;
y-=3;
System.out.println();
System.out.println("x= "+x+" y= "+y);
y/=x;
System.out.println("x= "+x+" y= "+y);
y++;
y%=x;
System.out.println("x= "+x+" y= "+y);
}
}
三目运算符
Java中的三墓运算符与C语言中的完全相同,使用形式为:
x?y:z;
这就是说,先计算x的值,若x为真,怎整个表达式的结果为y的值,若x为假,则整个表达式的值为表达式z的值。
例如:
int x=5,y=8,z=2;
int k=x<3?y:z; //因为x<3,所以k=z=2;
int j=x>0?x:-x //y的值始终为x的绝对值。