1.写一个程序,打印从1到100的值。
public static void main(String[] args) {
int n = 0;
while (++n <= 100) {
System.out.print(n+" ");
}
}
2.写一个程序,产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于,小于,或等于紧随它而随机产生的值。
public class Exercise {
public static void main(String[] args) {
test();
}
public static void test(){
int[] b = ProRandom(25);
for (int i = 0; i < b.length; i++) {
if (i == (b.length - 1)) {
System.out.println(b[i] + "是最后一个产生的数" );
break;
}
compare(b[i],b[i+1]);
}
}
public static void compare(int m,int n) {
if (m == n) {
System.out.println(m + "等于" + n);
}
else if (m > n) {
System.out.println(m + "大于" + n);
}
else {
System.out.println(m + "小于" + n);
}
}
public static int[] ProRandom(int amount){
Random rnd = new Random();
int[] b = new int[amount];
for (int i = 0; i < amount; i++) {
b[i] = rnd.nextInt();
}
return b;
}
}
3.修改练习2,把代码用一个while无限循环包括起来。然后运行它直至用键盘中断其运行(通常是通过按Ctrl-c)。
public class Exercise {
public static void main(String[] args) {
test();
}
public static void test(){
int[] b = ProRandom(25);
while (true) { //终止条件修改为true
for (int i = 0; i < b.length; i++) {
if (i == (b.length - 1)) {
System.out.println(b[i] + "是最后一个产生的数" );
break;
}
compare(b[i],b[i+1]);
}
}
}
public static void compare(int m,int n) {
if (m == n) {
System.out.println(m + "等于" + n);
}
else if (m > n) {
System.out.println(m + "大于" + n);
}
else {
System.out.println(m + "小于" + n);
}
}
public static int[] ProRandom(int amount){
Random rnd = new Random();
int[] b = new int[amount];
for (int i = 0; i < amount; i++) {
b[i] = rnd.nextInt();
}
return b;
}
}
4.写一个程序,使用两个嵌套的for循环和取余操作符(%)来探测和打印素数。
public class Exercise {
public static void main(String[] args) {
test();
}
public static void test(){
for (int i = 0; i < 100; i++) {
if (isPrimeNum(i)) {
System.out.println(i+"是素数");
}
}
}
public static boolean isPrimeNum(int num){
for (int i = 2; i < Math.abs(num); i++) {
if (num%i == 0) {
return false;
}
}
return true;
}
}
5.重复第三章中的练习10,不要用Integer.toBinaryString()方法,而是用三元操作符合按位操作符来显示二进制的1和0。
(3-10)编写一个具有两个常量值得程序,一个具有交替的二进制位1和0,其中最低有效位为0,另一个也具有交替的二进制位1和0,
但是其最低有效位为1.取这两个值,用按位操作符以所有可能的方式结合运算它们,然后用Integer.toBinaryString()显示。
public static void main(String[] args){
int a = 0xAAAA;
int b = 0x5555;
int c;
c = a & b;
outCBit(c);
c = a | b;
outCBit(c);
c = ~a;
outCBit(c);
c = a ^ b;
outCBit(c);
}
private static void outCBit(int c) {
int d = 0x8000;
for (int i = 0; i < 16; i++) {
int out = (c | d) == c ? 1 : 0;//循环比较得出每一位
System.out.print(out);
d>>>=1;
}
System.out.println();
}
6.略
7.修改本章练习1,通过使用break关键字,使得程序在打印到99时退出。然后尝试用return达到同样的目的。
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
if(i == 99){
break;
}
System.out.print(i+" ");
}
}
8.略
9.略
10.吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字:1260 = 21 * 60; 1827 = 21 * 87; 2187 = 27 * 81; 写一个程序,找出4位数的所有吸血鬼数字。
解答思路
1) 遍历从1001到9801所有四位数,给每个4位数从11开始做除法,直到除数和商符合规则。
2) 遍历从1001到9801所有四位数,按照规则将四位数的4个数字两两组合起来,然后各自相乘,找出符合规定的。
相比之下我们选择第二种方法:
public class Exercise {
public static void main(String[] args) {
test();
}
public static void test(){
int[] nums = new int[4];
for (int i = 1001; i < 9801; i++) {
getEachNum(i,nums);
int firstNumOne,secNumOne,secNumTwo;
for (int j = 0; j < 4; j++) {
firstNumOne = nums[0]*10+nums[1];
secNumOne = nums[2]*10+nums[3];
secNumTwo = nums[3]*10+nums[2];
isPass(firstNumOne,secNumOne,i);
isPass(firstNumOne,secNumTwo,i);
moveOneStep(nums);
}
}
}
//获取四位数的每一位数并放入数组
public static void getEachNum(int i,int[] args){
String str = Integer.toString(i);
for (int j = 0; j < 4; j++) {
args[j]= Integer.parseInt(String.valueOf(str.charAt(j)));
}
}
//判断是否符合题意
public static void isPass(int num1,int num2,int i){
if (i == num1 * num2) {
System.out.println("数字"+ i +"是吸血鬼数");
}
}
//将数组中元素向做移动一位,第一个数放到最后
public static void moveOneStep(int[] args){
int temp = args[0];
for (int i = 0; i < args.length; i++) {
if (i == args.length-1) {
args[args.length-1] = temp;
break;
}
args[i] = args[i+1];
}
}
}