码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA之路002-运算符

时间:2016-05-12 20:26:41      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

## 运算符
###++ -- 的用法
1. ++a 先赋值后运算
2. a++ 先运算后赋值
例如:

public class Suanshu2{

    public static void main(String[] args){

        int num = 1;  //声明一个变量
        int num2 = 5;
        int i = 1;

        System.out.println(num+num2);
        /*++ --*/
        /*

        num++  ++num; ->num = num+1;
        num--  --num; ->num = num-1;
        */

        num = num++;   //->num = num+1;  

        /*num++ 包含的意思为,先运算在自增   ++num先自增在运算*/ 
        
        num2 = ++num2;//6
        
        //num2 = num2+1;
        
        System.out.println("值为:"+i++*++i*i++);   //
        System.out.println("i="+i);   //
        
        System.out.println(num);
        System.out.println(num2);
        
        int j = 1;
        //System.out.println("j++结果:"+j++); //j = 1;   j=j+1   j=2
        //System.out.println(++j);  // 先运算j=j+1  在输出
        j = j++;  j = j+1;  
        int n = 1;
        n = n+j;
        System.out.println("1n"+n);
        n = n+j;
        System.out.println("2n"+n);
        
        
        //运算符优先级
        //.()->算数运算符->关系运算符->位运算符->逻辑运算符->三目运算符->赋值运算符。
    
    }

}

###位运算符
    public class Wei{
        public static void main(String[] args){
            /*&   位于
             *|   位或
             *^   位异
             *~   位补(位非)
             *<<  左移运算符
             *>>  右移
             *>>> 按右移补0操作符
             * */
            
            int a = 60;
            int b = 13;
            
            System.out.print("a&b ="+(a&b));//位与
            System.out.print("a|b ="+(a|b));//位或
            
            System.out.print("a^b ="+(a^b));//位异
            System.out.println("~a ="+(~a));//位补
            
            System.out.println("a<<2 ="+(a<<2));//位移
            System.out.println("a<<3 ="+(a<<3));//位移  左边移动增加移动的倍数。
            //位操作是效率最高的运算符。
            System.out.println("a>>2 ="+(a>>2)); //右位移。
            
            System.out.println("a>>>2 ="+(a>>>2)); //右位移,补0操作符,移动过后的空位补零填充。
            
            a+=1;   //-> a = a+1
            a-=1;   //-> a = a-1;
            a+=b;   //-> a = a+b;
            
            
            
        }
        
    }
###逻辑运算符
    public class Luoji{
        public static void main(String[] args){
            int year = 2000;
            System.out.print(!(year%4==0  && year %100 !=0));  //同时为真才返回真,否则假。
            if(year%4==0  && year %100 !=0){  //&& 且。逻辑与。
                
            }
            
            
            if((year%4==0  && year %100 !=0) || year %400 ==0){   // ||或 ;逻辑或。只要一个为真返回真
                
            }
            
            
            
            
        }
        
    }

###关系运算符
    public class Guanxi{
        public static void main(String[] args){
            /*关系运算符 >  < >=  <=  == !=*/
            
            System.out.print(3>2);  //关系比较得到两个结果  true false。
            
            /*
             * = 赋值运算符  
             * == 判断。
             * */
            
            int i = 1;
            if(i !=1){  //true  false
                
            }
            
            char  a = ‘a‘;  //->97
            char A = ‘A‘;   //->65
            System.out.print(a>A);  //比较ascii码。
            
            
            int d =1,b = 2,c = 3;
            
            //System.out.print((d>b)>(d>c));   boolean值boolean无法比较
            
            
        }
        
    }
###三目运算符
    import java.util.Scanner;
    
    public class Sanmu{
        public static void main(String[] args){
            /*三目运算符  三元运算符  条件运算符*/
            int age;
            Scanner s = new Scanner(System.in);
            System.out.print("请输入你的年龄");
            age = s.nextInt();
            
            String res = (age>18)?"欢迎来到洗浴中心":"未成年不得进入";  //age>18 
            
            System.out.print(res);
            
        }
        
    }
##课堂练习
求三个数的最大值

    import java.util.Scanner;
    
    public class AvgValue {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner s = new Scanner(System.in);
            System.out.println("请输入第一个数");
            int a = s.nextInt();
            int max = a;  //把第一次输入的值当作最大值,和后面的值比较
            
            System.out.println("请输入第二个数");
            int b = s.nextInt();
            max = (b>max)?b:max;  //b如果比max大 ,max = b;
            
            System.out.println("请输入第三个数");
            int c = s.nextInt();
            max = (c>max)?c:max;  //b如果比max大 ,max = c;
            
            System.out.print(max);
            
            s.close();  //关闭scanner对象输入。
            
    
            
        }
    
    }
自己做的,求最大值和平均数

    import java.util.Scanner;
    public class Sanmu{
    public static void main(String[] args){
     Scanner sc=new Scanner(System.in);
         int x=sc.nextInt();
         int y=sc.nextInt();
         int z=sc.nextInt();
         int a=(x>y)?x:y;
         double avg=(x+y+z)/3.0;
         System.out.print("最大的数是:");
         System.out.print((a>z)?a:z);
         System.out.print("平均值为:"+avg);
         sc.close();
         
    }
    }
##顺序结构
语句A→语句B
##选择结构
if语句 多分支条件语句
if(布尔表达式){//如果布尔表达式为真,就执行的语句
//花括号表示代码块
}
else if(布尔表达式){}
else{//当if不满足条件,进入否则代码块
}

嵌套IF
就是if中间在加上if语句
if(){
if(){
}
}else if(){}
例如给定一个数求星期几

    import java.util.Scanner;
    
    public class Time {
        public static void main(String[] args){
             Scanner s=new Scanner(System.in);
             System.out.print("请输入一个时间");
             int num= s.nextInt();
             if(num%7==1){
             System.out.print("星期一");
             }
             else if(num%7==2){
               System.out.print("星期二");
             }
              else if(num%7==3){
             System.out.print("星期三");
             }
             else if(num%7==4){
             System.out.print("星期四");
             }
              else if(num%7==5){
             System.out.print("星期五");
             }
              else if(num%7==6){
             System.out.print("星期六");
             }
              else {
             System.out.print("星期天");
             }
            
             s.close();
         }
        }
   

PS:根据昨天的习题,自己写的一个十进制转二进制的代码

    import java.util.Scanner;
    public class Er{
    public static void main(String[] agrs){
       Scanner S =new Scanner(System.in);
       int num=S.nextInt();
       String str="";
       int i;
     while(num>0){
       i=num%2;
       str=i+str;
       num=num/2;
       }
       System.out.print(str);
    }
    }

JAVA之路002-运算符

标签:

原文地址:http://blog.csdn.net/mrjintao/article/details/51356231

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!