y·c="2" 实际上会修改代()外面的对象。 expect()里面的输出可以印证这 Aliasing及其解决方案是个很复杂的课题。虽然我们要到附录A才会作 全面讲解,但是你现在就得知道有这个问题,这样才会注意到 阱 数学运算符 在绝大多数编程语言里面,基本的数学运算符都是相同的:加(+),减( ),除(/),乘(*),取模(%,它会返回整数相除之后的余数)。整数相 除,会把运算结果的小数点后面全部截掉,而不是作四舍五入 Java也有可以同时进行计算和赋值的简写符号。它用运算符加一个等号 来表示,在Java语言中,这种表示方法能一致地用于所有的运算符(只 要是有意义就行了)。比如,你要为变量x加4,然后再赋给x,就写:x 下面这个例程演示了数学运算符的用法: //: c03: Mathops. java / Demonstrates the mathematical operators mport com. bruceeckel. simpletest. import java.util public class Mathops static Test monitor new Test o // Shorthand to print a string and an int: c void printInt (stri int 1) System. out. println( / Shorthand to print a string and a float static void printFloat (string s, float f)t System. out. println(s+ public static void main(String[] args)t // Create a random number generator, / seeds with current time by default Random rand new Random()i / Choose value from 1 to 100 rand. nextInt(100)+ 1 k rand. nextInt(100)+1 printInt("i",3)i printInt ("k",k) Int("3+k",i ki printInt("3-k", i i =k,ii printInt("k/i", i i =k* ji printInt("k*i", i) i=k号j; printing("k号j",i) 第6页共48页shhgs@wgqqh.com 6 ✁ ✂ 48 ✁ y.c = 'z'; ñJºïð f( )½¶(¥_expect( )˶(©!ÝEªÕ 3 C Al iasin g <§£Nµ>×Ö×(4dØ|W%g,5 A Ùº1 c¶©£¡íFÉm ×cd ¦Ùº¹g3ÌÍÚ ÛÏ FÂV"áâ˶G ("F Æ>(0ä(+)P(- )R(/)Q(*)ÜÝ(AºÞ¼¤">Ru(o")¤"> RºPOÅ(ß"C¶c{¹àX1àáât Java [mÝE(@~L()« SA ä3×-S QhF Java * hµ;3ãr)Dm( (ó %m¹ä, )8Ä¡% x ä 4|Á~¬ x«0x += 4 ¶ ×]â,"F (0 //: c03:MathOps.java // Demonstrates the mathematical operators. import com.bruceeckel.simpletest.*; import java.util.*; public class MathOps { static Test monitor = new Test(); // Shorthand to print a string and an int: static void printInt(String s, int i) { System.out.println(s + " = " + i); } // Shorthand to print a string and a float: static void printFloat(String s, float f) { System.out.println(s + " = " + f); } public static void main(String[] args) { // Create a random number generator, // seeds with current time by default: Random rand = new Random(); int i, j, k; // Choose value from 1 to 100: j = rand.nextInt(100) + 1; k = rand.nextInt(100) + 1; printInt("j", j); printInt("k", k); i = j + k; printInt("j + k", i); i = j - k; printInt("j - k", i); i = k / j; printInt("k / j", i); i = k * j; printInt("k * j", i); i = k % j; printInt("k % j", i); j %= k; printInt("j %= k", j);