Scala - 算术运算符



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

示例

object Demo {
   def main(args: Array[String]) {
      var a = 10;
      var b = 20;
      var c = 25;
      var d = 25;
      
      println("a + b = " + (a + b) );
      println("a - b = " + (a - b) );
      println("a * b = " + (a * b) );
      println("b / a = " + (b / a) );
      println("b % a = " + (b % a) );
      println("c % a = " + (c % a) );
   }
}

将上述程序保存为 **Demo.scala**。以下命令用于编译和执行此程序。

命令

\>scalac Demo.scala
\>scala Demo

输出

a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
scala_operators.htm
广告