Scala 赋值运算符

Scala 赋值运算符

尝试以下示例程序以了解Scala编程语言中所有可用的位运算符。

示例

object Demo {
   def main(args: Array[String]) {
      var a = 10;   
      var b = 20;
      var c = 0;

      c = a + b;
      println("c = a + b  = " + c );

      c += a ;
      println("c += a  = " + c );

      c -= a ;
      println("c -= a = " + c );

      c *= a ;
      println("c *= a = " + c );

      a = 10;
      c = 15;
      c /= a ;
      println("c /= a  = " + c );

      a = 10;
      c = 15;
      c %= a ;
      println("c %= a  = " + c );

      c <<= 2 ;
      println("c <<= 2  = " + c );

      c >>= 2 ;
      println("c >>= 2  = " + c );

      c >>= 2 ;
      println("c >>= 2  = " + c );

      c &= a ;
      println("c &= a  = " + c );

      c ^= a ;
      println("c ^= a  = " + c );

      c |= a ;
      println("c |= a  = " + c );
   }
}

将上面的程序保存在 Demo.scala 中。以下命令用于编译和执行该程序。

命令

>scalac Demo.scala
\>scala Demo

输出

c = a + b  = 30
c += a  = 40
c -= a  = 30
c *= a  = 300
c /= a  = 1
c %= a  = 5
c <<= 2  = 20
c >>= 2  = 5
c >>= 2  = 1
c &= a  = 0
c ^= a  = 10
c |= a  = 10

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程