第四章 没啥好说的......
4.1
1 package ex0401; 2 //[4.1]写一个程序打印从1到100的值 3 4 public class PrintOneToHundred 5 { 6 public static void main(String[] args) 7 { 8 for(int i = 1; i <= 100; ++i) 9 System.out.print(i + " "); 10 } 11 }
4.1
4.2-4.3
1 package ex040203; 2 //[4.2]写一个程序,产生25个int类型的随机数.对于每一个随机值,使用if-else语句来讲其分类为大于\小于,或等于紧随它产生的值 3 //[4.3]修改练习2,把代码用一个while无限循环包括起来.然后运行它直至键盘中断其运行(通常是通过按Ctrl+C). 4 5 import java.util.Random; 6 7 public class GetRandomInteger 8 { 9 public static void main(String[] args) 10 { 11 int i = 0; 12 Random rand = new Random(); 13 int curr = rand.nextInt(); 14 int prev = 0; 15 //[4.2] 16 while(++i < 25) 17 //[4.3] 18 //while(true) 19 { 20 prev = rand.nextInt(); 21 System.out.print(curr + " "); 22 if(curr > prev) 23 System.out.print("greater than "); 24 else if(curr < prev) 25 System.out.print("less than "); 26 else 27 System.out.print("equals "); 28 curr = prev; 29 } 30 System.out.print(prev); 31 } 32 }
4.2-4.3
4.4
1 package ex0404; 2 //[4.4]写一个程序,使用两个嵌套的for循环和取余操作符来探测和打印素数 3 4 public class GetPrime 5 { 6 public static void main(String[] args) 7 { 8 System.out.println(2); 9 for(int number = 3; number < 10000; number += 2) 10 { 11 int index; 12 for(index = 3; index < number ; index += 2) 13 { 14 if(number % index == 0) 15 break; 16 } 17 if(index * index > number) 18 System.out.println(number); 19 } 20 } 21 }
4.4
4.5
1 package ex0405; 2 //重复练习[3.10],不要用Integer.toBinaryString()方法,而是用三元操作符合按位操作符来显示二进制的1和0 3 4 public class AnotherBitOperation 5 { 6 public static void main(String[] args) 7 { 8 int first = 0x2AA; 9 int second =0x133; 10 11 for(int i = 0; ((first >> i) & 0xFFFF) != 0; ++i) 12 System.out.print((first >> i ) & 1); 13 System.out.println(""); 14 for(int i = 0; ((second >> i) & 0xFFFF) != 0; ++i) 15 System.out.print((second >> i ) & 1); 16 System.out.println(""); 17 for(int i = 0; (((first & second) >> i) & 0xFFFF) != 0; ++i) 18 System.out.print(((first & second) >> i ) & 1); 19 System.out.println(""); 20 for(int i = 0; (((first | second) >> i) & 0xFFFF) != 0; ++i) 21 System.out.print(((first | second) >> i ) & 1); 22 System.out.println(""); 23 for(int i = 0; (((first ^ second) >> i) & 0xFFFF) != 0; ++i) 24 System.out.print(((first ^ second) >> i ) & 1); 25 System.out.println(""); 26 } 27 }
4.5
4.7
1 package ex0407; 2 //[4.7]修改本章练习1,通过使用break关键词,使得程序在打印到99时退出.然后尝试用return达到相同目的 3 4 public class TestBreakAndReturn 5 { 6 public static void main(String[] args) 7 { 8 for(int i = 1; i <= 100; ++i) 9 { 10 if(i == 100) 11 return; 12 //break; 13 System.out.print(i + " "); 14 } 15 } 16 }
4.7
4.8
1 package ex0408; 2 //[4.8]写一个switch开关语句,为每个case打印一个消息.然后把这个switch放进for循环来测试每个case. 3 //先让每个case后面都有break,测试一下会怎么样;然后把break删除,看看会怎样. 4 5 public class TestSwitch 6 { 7 public static void main(String[] args) 8 { 9 for(int i = 0; i < 3; ++i) 10 { 11 switch(i) 12 { 13 case 0: 14 System.out.println("Cat");//break; 15 case 1: 16 System.out.println("Dog");//break; 17 case 2: 18 System.out.println("Bird");//break; 19 default: 20 break; 21 } 22 } 23 } 24 }
4.8
4.9
1 package ex0409; 2 //[4.9]一个斐波那契数列是由数字从第三个数字其都是前两个数字的和. 3 //创建一个方法,接收一个整数参数,并显示从第一个元素开始,总共有该参数指定的个数所构成的所有斐波那契数字. 4 5 class Fibonacci 6 { 7 private static final int FIRST = 1; 8 private static final int SECOND = 1; 9 10 public static void printFibonacci(int length) 11 { 12 if(length <= 0) 13 System.out.println("error!"); 14 switch(length) 15 { 16 case 2: 17 System.out.println(FIRST); 18 case 1: 19 System.out.println(SECOND);break; 20 default: 21 { 22 int prevprev = FIRST; 23 int prev = SECOND; 24 int curr; 25 System.out.println(FIRST); 26 System.out.println(SECOND); 27 for(int i = 3; i <= length; ++i) 28 { 29 curr = prevprev + prev; 30 System.out.println(curr); 31 prevprev = prev; 32 prev = curr; 33 } 34 } 35 } 36 } 37 } 38 public class ShowFibonacci 39 { 40 public static void main(String[] args) 41 { 42 Fibonacci.printFibonacci(10); 43 } 44 }
4.9
时间: 2024-10-05 16:52:56