目录
- 4.1 true和false
- 4.2 if-else
- 4.3 迭代
- 4.3.1 do-while
- 4.3.2 for
- 练习1
- 练习2
- 练习3
- 练习4
- 练习5
- 4.3.3 逗号操作符
- 4.4 Foreach语句
- 4.5 rerurn
- '练习6
- 4.6 break和continue
- 练习7
- 4.7 臭名昭著的goto
- 4.8 switch
- 练习8
- 练习9
- 练习10
4.1 true和false
4.2 if-else
4.3 迭代
public class Test01 {
public static void main(String[] args) {
System.out.println("start work...");
while (condition()) {
System.out.println("do working...");
}
System.out.println("end work...");
}
static boolean condition() {
boolean result = Math.random() < 0.99;
System.out.println("result = " + result);
return result;
}
}
4.3.1 do-while
do {
statement
}while(Boolean-expression)
4.3.2 for
for (initialization; Boolean-expression; step) {
statement
}
练习1
public class Exec01 {
public static void main(String[] args) {
for(int i = 1; i <= 100; i++ )
System.out.println(i);
}
}
练习2
import java.util.Random;
public class Exec02 {
public static void main(String[] args) {
Random r = new Random();
int before = r.nextInt(100);
int current = r.nextInt(100);
for (int i = 0; i < 25; i++) {
System.out.print("before=" + before + ", current=" + current + ": ");
if (before < current) System.out.println("大于");
else if (before > current) System.out.println("小于");
else System.out.println("等于");
before = current;
current = r.nextInt(100);
}
}
}
练习3
import java.util.Random;
public class Exec02 {
public static void main(String[] args) {
Random r = new Random();
int before = r.nextInt(100);
int current = r.nextInt(100);
while ()
for (int i = 0; i < 25; i++) {
System.out.print("before=" + before + ", current=" + current + ": ");
if (before < current) System.out.println("大于");
else if (before > current) System.out.println("小于");
else System.out.println("等于");
before = current;
current = r.nextInt(100);
}
}
}
练习4
public class Exec04 {
public static void main(String[] args) {
boolean flag = true;
for (int i = 2; i <= 100; i++) {
for (int j = 2; j < i; j++)
if (i % j == 0) {
flag = false;
break;
}
if (flag) System.out.println(i);
flag = true;;
}
}
}
练习5
public class Exec05 {
public static void main(String[] args) {
String result = method1(10, "");
System.out.println(result);
}
public static String method1(int a, String s) {
return a > 0 ? method1(a >>> 1, (a & 1) + s) : s;
}
}
4.3.3 逗号操作符
注意是逗号操作符,而不是逗号分隔符。
- 逗号分隔符:用来分隔函数的不同参数的。
- 逗号操作符:用在fior循环的控制表达式中。在控制表达式的初始化和步进控制部分。可以使用一个或几个逗号分隔语句,这些语句会独立执行。
public class Test02 {
public static void main(String[] args) {
for(int i = 1, j = i + 10; i < 5; i++, j = i * 2) {
System.out.println("i=" + i + ", j=" + j);
}
}
}
初始化部分可以定义任意数量的相同类型变量。
4.4 Foreach语句
javase5引入的foreeach语句语法。不用创建int变量,就可以便利数值和容器、
4.5 rerurn
java中表示无条件分支关键词:return
、break
、continue
、goto。
return
的用途
- 指定一个方法的返回什么值;
- 结束方法,返回那个值。
public class Test03 {
static int test(int testval. int target) {
if(testval > target) return +1;
else if(test < target) return -1;
else return 0; // 不用加else 因为前面的if语句都有return语句,都可以终止执行。
}
public static void main(String[] args) {
System.out.println(test(10, 5));
}
}
如果在返回值是void的方法中,没有return语句,系统会在该方法的结尾处添加一个隐式的return。
'练习6
// P135
public class Exec06 {
public static void main(String[] args) {
IfElse.test(10, 5, 1, 20);
System.out.println(IfElse2.test(10, 5, 1, 20));
}
}
class IfElse {
static int result = 0;
static void test(int testval, int target, int begin, int end) {
if (testval > begin && testval < end) System.out.println(testval + "在(" + begin + ", " + end + ")之间") ;
if(testval > target) result = +1;
else if(testval < target) testval = -1;
else result = 0;
}
}
class IfElse2 {
static int test(int testval, int target, int begin, int end) {
if (testval > begin && testval < end) System.out.println(testval + "在(" + begin + ", " + end + ")之间") ;
if(testval > target) return +1;
else if(testval < target) return -1;
else return 0;
}
}
4.6 break和continue
练习7
public class Exec07 {
/*public static void main(String[] args) {
for(int i = 1; i <= 100; i++ ) {
System.out.println(i);
if (i == 99) break;
}
}*/
public static void main(String[] args) {
for(int i = 1; i <= 100; i++ ) {
System.out.println(i);
if (i == 99) return;
}
}
}
4.7 臭名昭著的goto
- 一般的continue会退回最内层循环的开头(顶部),并继续执行。
- 带标签的continue会到达标签的位置,并重新进入紧接在哪个标签后面的循环。
- 一般的break会中断并跳出当前的循环。
- 带标签的break会中断并跳出标签所致的循环。
4.8 switch
switch
的格式如下:
switch(integral-selector) {
case integral-value1 : statement; break;
case integral-value2 : statement; break;
case integral-value3 : statement; break;
// ...
default: statement;
}
integral-selector(整数选择因子),是一个表达式,它能否产生整数值。如果iswitch会将ntegral-selector的值与多个integral-value(整数值)相比较,相等就执行对应的statement(单个语句,或者是多条语句,不需要用括号括起来)。每个case的后面都有一个break语句。但是最后一个default后面没有break语句。
integral-selector可以是int
、char
、enum
类型。
import java.util.Random;
// 随机生成字母,并判断他们是元音还是辅音字母
// A的ASCII码是65,a的ASCII码是97
public class Test03 {
public static void main(String[] args) {
Random r = new Random(10);
char ch;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) ch = (char) (r.nextInt(26) + 65);
else ch = (char) (r.nextInt(26) + 97);
System.out.print(ch + ", " + (int)ch + " : ");
switch (Character.toUpperCase(ch)) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': System.out.println("vowel"); break;
case 'Y':
case 'W':
System.out.println("sometimes a vowel"); break;
default: System.out.println("consonant");
}
}
}
}
// 运行结果
P, 80 : consonant
g, 103 : consonant
L, 76 : consonant
y, 121 : sometimes a vowel
E, 69 : vowel
s, 115 : consonant
F, 70 : consonant
m, 109 : consonant
V, 86 : consonant
c, 99 : consonant
练习8
import java.util.Random;
public class Exec08 {
public static void main(String[] args) {
Random r = new Random(10);
// 产生[2, 7)的整数值
/*
for (int i = 0; i < 2; i++) {
int a = r.nextInt(7 - 2) + 2;
switch(a) {
case 2: System.out.println("222"); break;
case 3: System.out.println("333"); break;
case 4: System.out.println("444"); break;
case 5: System.out.println("555"); break;
default: System.out.println("666");
}
}
/*
运行结果:
555
222
*/
for (int i = 0; i < 2; i++) {
int a = r.nextInt(7 - 2) + 2;
System.out.print("a=" + a + " : ");
switch(a) {
case 2: System.out.println("222");
case 3: System.out.println("333");
case 4: System.out.println("444");
case 5: System.out.println("555");
default: System.out.println("666");
}
}
/*
运行结果
a=5 : 555
666
a=2 : 222
333
444
555
666
*/
}
}
练习9
public class Exec09 {
public static void main(String[] args) {
int n = args.length > 0 ? Integer.parseInt(args[0]) : 5;
findFib(n);
}
public static void findFib(int n) {
switch (n) {
case 1 : System.out.println(1); break;
case 2 : System.out.println("1、1"); break;
default:
StringBuilder builder = new StringBuilder("1、1、");
for (int i = 3; i <= n; i++) {
int fib = fib(i);
builder.append(fib);
builder.append("、");
}
System.out.println(builder.deleteCharAt(builder.length() - 1));
}
}
private static int fib(int n) {
if (n <= 1) return 1;
return fib(n-1) + fib(n-2);
}
}
// 运行结果
1、1、3、5、8
练习10
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Exec10 {
public static void main(String[] args) {
Set<Object> set = new HashSet<>();
for (int i = 1001; i < 10000; i++) {
int a = i / 1000;
int b = i % 1000 / 100;
int c = i % 100 / 10;
int d = i % 10;
if (c == 0 && d == 0) continue;
if (i == 6880)
System.out.println();
if (i == (a * 10 + b) * (c * 10 + d) && c != 0) set.add(i);
if (i == (a * 10 + b) * (d * 10 + c) && d != 0) set.add(i);
if (i == (a * 10 + c) * (b * 10 + d) && b != 0) set.add(i);
if (i == (a * 10 + c) * (d * 10 + b) && d != 0) set.add(i);
if (i == (a * 10 + d) * (b * 10 + c) && b != 0) set.add(i);
if (i == (a * 10 + d) * (c * 10 + b) && c != 0) set.add(i);
if (b != 0) {
if (i == (b * 10 + a) * (c * 10 + d) && c != 0) set.add(i);
if (i == (b * 10 + a) * (d * 10 + c) && d != 0) set.add(i);
if (i == (b * 10 + c) * (d * 10 + a) && d != 0) set.add(i);
if (i == (b * 10 + d) * (c * 10 + a) && c != 0) set.add(i);
}
if (c != 0) {
if (i == (c * 10 + a) * (b * 10 + d) && b != 0) set.add(i);
if (i == (c * 10 + a) * (d * 10 + b) && d != 0) set.add(i);
if (i == (c * 10 + d) * (b * 10 + a) && b != 0) set.add(i);
}
if (d != 0) {
if (i == (d * 10 + b) * (c * 10 + a) && c != 0) set.add(i);
if (i == (d * 10 + c) * (b * 10 + a) && b != 0) set.add(i);
if (i == (d * 10 + a) * (b * 10 + c) && b != 0) set.add(i);
if (i == (d * 10 + a) * (c * 10 + b) && c != 0) set.add(i);
}
}
Object[] arr = set.toArray();
Arrays.sort(arr);
for (Object i : arr) {
System.out.println(i);
}
}
}
// 运行结果
1260
1395
1435
1530
1827
2187
6880