一、写一个程序打印1到100的值。
public class Main {
public static void main(String[] agrs) {
int i = 0;
while (i < 100) {
System.out.print(++i + ",");
}
}
}
返回:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,
二、写一个程序,产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于、小于,或等于紧随它的而随机生成的值。
public class Main {
public static void main(String[] agrs) {
Random random = new Random();
int i = 1;
int preNum = 0;
do {
if (preNum != 0) {
int nextNum = random.nextInt(100);
if (preNum < nextNum) {
System.out.println("pre=" + preNum + "比next=" + nextNum + "小。");
} else if (preNum > nextNum) {
System.out.println("pre=" + preNum + "比next=" + nextNum + "大。");
} else {
System.out.println("pre=" + preNum + "与next=" + nextNum + "相等。");
}
preNum = nextNum;
} else {
preNum = random.nextInt(100);
}
i++;
} while (i < 25);
}
返回:
pre=54比next=84小。 pre=84比next=6大。 pre=6比next=78小。 pre=78比next=24大。 pre=24比next=89小。 pre=89比next=66大。 pre=66比next=54大。 pre=54与next=54相等。 pre=54比next=78小。 pre=78比next=93小。 pre=93比next=41大。 pre=41比next=22大。 pre=22比next=45小。 pre=45比next=76小。 pre=76比next=18大。 pre=18比next=67小。 pre=67比next=95小。 pre=95比next=97小。 pre=97比next=4大。 pre=4比next=62小。 pre=62比next=70小。 pre=70比next=59大。 pre=59比next=46大。
三、修改“练习二”,把代码用while()无限循环包括起来。然后运行它直至用键盘中断其运行。
public class Main {
public static void main(String[] agrs) {
Random random = new Random();
int preNum = 0;
while (true) {
if (preNum != 0) {
int nextNum = random.nextInt(100);
if (preNum < nextNum) {
System.out.println("pre=" + preNum + "比next=" + nextNum + "小。");
} else if (preNum > nextNum) {
System.out.println("pre=" + preNum + "比next=" + nextNum + "大。");
} else {
System.out.println("pre=" + preNum + "与next=" + nextNum + "相等。");
}
preNum = nextNum;
} else {
preNum = random.nextInt(100);
}
}
}
}
四、写一个程序,使用两个嵌套的for循环和取余操作符(%)来探测和打印素数(只能被其自身和1整除,而不能被其它数字整除的整数)。
public class Main {
public static void main(String[] agrs) {
for (int i = 1; i < 100; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
continue;
}
}
if (flag) {
System.out.print(i + ",");
}
}
}
}
返回:
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
五、重复第3章中的练习10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符来显示二进制的1和0。
public class Main {
public static void main(String[] agrs) {
int num1 = 0xAA; // 10101010
int num2 = 0x55; // 1010101
outMethod("1. 10101010 & 1010101: ", (num1 & num2));
outMethod("2. 10101010 | 1010101: ", (num1 | num2));
outMethod("3. 10101010 ^ 1010101: ", (num1 ^ num2));
outMethod("4. ~10101010: ", (~num1));
num2 &= num1;
outMethod("5. &= 10101010: ", (num2));
num2 = 0x55;
num2 |= num1;
outMethod("6. |= 10101010: ", (num2));
num2 = 0x55;
num2 ^= num1;
outMethod("7. ^= 10101010: ", (num2));
}
private static void outMethod(String str, int i) {
System.out.print(str);
if (i != 0) {
int temp = 0x8000; // 1000000000000000
int len = 8;
while ((i < 0 ? (~i + 1) << 1 : i) >> len != 0) {
len <<= 1;
}
for (int j = 0; j < len; j++) {
int num = (i | temp) == i ? 1 : 0;
temp >>>= 1;
System.out.print(num);
}
System.out.println();
} else {
System.out.println(0);
}
}
}
返回:
1. 10101010 & 1010101: 0 2. 10101010 | 1010101: 00000000 3. 10101010 ^ 1010101: 00000000 4. ~10101010: 1111111101010101 5. &= 10101010: 0 6. |= 10101010: 00000000 7. ^= 10101010: 00000000
六、修改前两个程序中的test(),让它们接受两个额外的参数begin和end,这样在测试testval时将判断它是否在begin和end之间(含)的范围内。
public class Main {
public static void main(String[] args) {
System.out.println(test(10, 20, 15, 5));
System.out.println(test(10, 20, 4, 5));
System.out.println(test(10, 20, 21, 5));
}
static int test(int begin, int end, int testval, int target) {
if (testval < begin) {
System.out.println(testval + " is lower than begin(" + begin + ")");
} else if (testval > end) {
System.out.println(testval + " is bigger than end(" + end + ")");
} else if (testval > begin && testval < end) {
System.out.println(testval + " is between begin(" + begin + ") and end(" + end + ")");
}
if (testval > target) {
return +1;
} else if (testval < target) {
return -1;
} else {
return 0;
}
}
}
返回:
15 is between begin(10) and end(20) 1 4 is lower than begin(10) -1 21 is bigger than end(20) 1
七、写一个switch开关语句,为每一个case打印一个消息。然后把这个switch放到for循环来测试每一个case。先让每一个case后面都有一个break,测试一下会怎样;然后把break删了,看看会怎样。
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
System.out.println("0:" + i);
break;
case 1:
System.out.println("1:" + i);
break;
case 2:
System.out.println("2:" + i);
break;
case 3:
System.out.println("3:" + i);
break;
case 4:
System.out.println("4:" + i);
break;
default:
System.out.println("5:" + i);
break;
}
}
}
}
返回:
0:0 1:1 2:2 3:3 4:4
八、一个斐波那契数列是由数字1、1、2、3、5、8、13、21、34等组成的,其中每个数字(从第三个数字起)都是前两个数字之和。创建一个方法,接受一个整数参数,并显示从第一个元素开始总共由该参数指定的个数所构成的所有斐波那契数字。例如:如果允许java Fibonacci 5(其中Fibonacci是类名),那么输出就应该是1、1、2、3、5。
public class Main {
public static void main(String[] args) {
outNum(9);
}
private static void outNum(int num) {
int pre = 0;
int next = 1;
StringBuilder str = new StringBuilder();
for (int i = 0; i < num; i++) {
if (i == 0) {
str.append(next);
} else {
str.append("、" + (pre + next));
int temp = pre + next;
pre = next;
next = temp;
}
}
System.out.println(str.toString());
}
}
返回:
1、1、2、3、5、8、13、21、34
九、吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如:下列数字都是“吸血鬼”数字:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81
写一个程序,找出4位数的所有吸血鬼数字。(待续)
来源:https://www.cnblogs.com/jojo-wang/p/11988430.html