练习1: (1)写一个程序,打印从1到100的值。
package com. laputa. chapter4. practice1;
public class Count {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
System. out. print ( i + " " ) ;
}
}
}
练习2: (2)写一个程序,产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于、小于,或等于紧随它而随机生成的值。
package com. laputa. chapter4. practice2;
import java. util. Random;
public class RandomInts {
static Random r = new Random ( 47 ) ;
public static void compareRand ( ) {
int a = r. nextInt ( ) ;
int b = r. nextInt ( ) ;
System. out. println ( "a = " + a + ", b = " + b) ;
if ( a < b) {
System. out. println ( "a < b" ) ;
} else if ( a > b) {
System. out. println ( "a > b" ) ;
} else {
System. out. println ( "a = b" ) ;
}
}
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 25 ; i++ ) {
compareRand ( ) ;
}
}
}
练习3: (1) 修改练习2,把代码用一个while无限循环包括起来。然后运行它直至用键盘中断其运行(通常是通过按Ctrl-C)。
package com. laputa. chapter4. practice3;
import com. laputa. chapter4. practice2. RandomInts;
public class RandomInts2 {
public static void main ( String[ ] args) {
while ( true ) {
RandomInts. compareRand ( ) ;
}
}
}
练习4: (3) 写一个程序,使用两个嵌套的for循环和取余操作符(%)来探测和打印素数(只能被其自身和1整除,而不能被其他数字整除的整数)。
package com. laputa. chapter4. practice4;
public class Primes {
public static void main ( String[ ] args) {
for ( int i = 1 ; i < 1000 ; i++ ) {
int factors = 0 ;
for ( int j = 1 ; j < ( i + 2 ) / 2 ; j++ ) {
if ( ( i % j) == 0 ) {
factors++ ;
}
}
if ( factors < 2 ) {
System. out. println ( i + " is prime" ) ;
}
}
}
}
练习5: (4) 重复第3章中的练习10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符来显示二进制的1和0。
package com. laputa. chapter4. practice5;
public class BitwiseOperators {
public static void main ( String[ ] args) {
int i1 = 0xaaaaaaaa ;
int i2 = 0x55555555 ;
System. out. print ( "i1 = " ) ;
toBinaryString ( i1) ;
System. out. println ( ) ;
System. out. print ( "i2 = " ) ;
toBinaryString ( i2) ;
}
private static void toBinaryString ( int i) {
char [ ] buffer = new char [ 32 ] ;
int index = 32 ;
do {
buffer[ -- index] = ( ( i & 0x01 ) != 0 ) ? '1' : '0' ;
i >>>= 1 ;
} while ( i != 0 ) ;
for ( int j = index; j < 32 ; j++ ) {
System. out. print ( buffer[ j] ) ;
}
}
}
练习6: (2)修改前两个程序中的两个test()方法,让它们接受两个额外的参数begin和end,这样在测试testvalI时将判断它是否在begin和end之间(包括begin和end) 的范围内。
package com. laputa. chapter4. practice6;
public class E06_RangeTest2 {
static boolean test ( int testval, int begin, int end) {
if ( testval >= begin && testval <= end) {
return true ;
}
return false ;
}
public static void main ( String[ ] args) {
System. out. println ( test ( 10 , 5 , 15 ) ) ;
System. out. println ( test ( 5 , 10 , 15 ) ) ;
System. out. println ( test ( 5 , 5 , 5 ) ) ;
}
}
练习7: (1) 修改本章练习1,通过使用break关键词,使得程序在打印到99时退出。然后尝试使用return来达到相同的目的。
package com. laputa. chapter4. practice7;
public class E07_To98 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
if ( i == 99 ) {
break ;
}
System. out. print ( i + " " ) ;
}
}
}
练习8: (2)写一个switch开关语句,为每个case打印一个消息。然后把这个switch放进for循环来测试每个case。先让每个case后面都有break,测试一下 会怎样;然后把break删了,看看会怎样。.
package com. laputa. chapter4. practice8;
public class E08_SwitchDemo {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 7 ; i++ ) {
switch ( i) {
case 1 : System. out. println ( "case 1" ) ;
break ;
case 2 : System. out. println ( "case 2" ) ;
break ;
case 3 : System. out. println ( "case 3" ) ;
break ;
case 4 : System. out. println ( "case 4" ) ;
break ;
case 5 : System. out. println ( "case 5" ) ;
break ;
default : System. out. println ( "default" ) ;
}
}
}
}
package com. laputa. chapter4. practice8;
public class E08_SwitchDemo2 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < 7 ; i++ ) {
switch ( i) {
case 1 : System. out. println ( "case 1" ) ;
case 2 : System. out. println ( "case 2" ) ;
case 3 : System. out. println ( "case 3" ) ;
case 4 : System. out. println ( "case 4" ) ;
case 5 : System. out. println ( "case 5" ) ;
default : System. out. println ( "default" ) ;
}
}
}
}
练习9: (4) 一个斐波那契数列是由数字1、1、 2、3、5、8、13. 21、34等等组成的,其中每一个数字(从第三个数字起)都是前两个数字的和。创建一个方法, 接受一个整数参数, 并显示从第一个元素开始总共由该参数指定的个数所构成的所有斐波那契数字。例如,如果运行java Fibonacci 5 (其中Fibonacci是类名),那么输出就应该是1、1. 2、3、5。
package com. laputa. chapter4. practice9;
public class E09_Fibonacci {
static int fib ( int n) {
if ( n <= 2 ) {
return 1 ;
}
return fib ( n- 1 ) + fib ( n- 2 ) ;
}
public static void main ( String[ ] args) {
int n = 5 ;
for ( int i = 1 ; i <= n; i++ ) {
System. out. print ( fib ( i) + ", " ) ;
}
}
}
练习10: (5) 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到, 而这对数字各包含乘积的一半位数的数字, 其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼"数字:
1260=21 * 60
1827=21 * 87
2187 =27* 81
写一个程序,找出4位数的所有吸血鬼数字(Dan Forhan推荐)。
package com. laputa. chapter4. practice10;
public class E10_Vampire {
public static void main ( String[ ] args) {
int [ ] startDigit = new int [ 4 ] ;
int [ ] productDigit = new int [ 4 ] ;
for ( int num1 = 10 ; num1 <= 99 ; num1++ ) {
for ( int num2 = num1; num2 <= 99 ; num2++ ) {
int product = num1 * num2;
startDigit[ 0 ] = num1 / 10 ;
startDigit[ 1 ] = num1 % 10 ;
startDigit[ 2 ] = num2 / 10 ;
startDigit[ 3 ] = num2 % 10 ;
productDigit[ 0 ] = product / 1000 ;
productDigit[ 1 ] = ( product % 1000 ) / 100 ;
productDigit[ 2 ] = product % 1000 % 100 / 10 ;
productDigit[ 3 ] = product % 1000 % 100 % 10 ;
int count = 0 ;
for ( int x = 0 ; x < 4 ; x++ ) {
for ( int y = 0 ; y < 4 ; y++ ) {
if ( productDigit[ x] == startDigit[ y] ) {
count++ ;
productDigit[ x] = - 1 ;
startDigit[ y] = - 2 ;
if ( count == 4 ) {
System. out. println ( num1 + " * " + num2
+ " : " + product) ;
}
}
}
}
}
}
}
}