当前位置: 首页 > 文档资料 > 学习 Java 编程 >

Show 例子 1

优质
小牛编辑
131浏览
2023-12-01

以下程序是一个演示算术运算符的简单示例。 将以下Java程序复制并粘贴到Test.java文件中,然后编译并运行该程序 -

例子 (Example)

public class Test {
   public static void main(String args[]) {
      int a = 10;
      int b = 20;
      int c = 25;
      int d = 25;
      System.out.println("a + b = " + (a + b) );
      System.out.println("a - b = " + (a - b) );
      System.out.println("a * b = " + (a * b) );
      System.out.println("b/a = " + (b/a) );
      System.out.println("b % a = " + (b % a) );
      System.out.println("c % a = " + (c % a) );
      System.out.println("a++   = " +  (a++) );
      System.out.println("b--   = " +  (a--) );
      // Check the difference in d++ and ++d
      System.out.println("d++   = " +  (d++) );
      System.out.println("++d   = " +  (++d) );
   }
}

这将产生以下结果 -

输出 (Output)

a + b = 30
a - b = -10
a * b = 200
b/a = 2
b % a = 0
c % a = 5
a++   = 10
b--   = 11
d++   = 25
++d   = 27