Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e69d019

Browse files
committed
chapter4
1 parent 9f1b14f commit e69d019

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter4;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 数组排序,考察调用API
7+
*/
8+
public class ArraySort {
9+
10+
public static void main(String[] args) {
11+
int[] number = {70, 80, 31, 37, 10, 1, 48, 60, 33, 80};
12+
13+
Arrays.sort(number);
14+
15+
for (int val: number) {
16+
System.out.println(val);
17+
}
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter4;
2+
3+
/**
4+
* 斐波那契数列
5+
*/
6+
public class Fibonacci {
7+
8+
private static int run(int num) {
9+
if (num == 1 || num == 2) {
10+
return 1;
11+
}
12+
13+
return run(num -1) + run(num - 2);
14+
}
15+
16+
public static void main(String[] args) {
17+
int result = run(9);
18+
System.out.println(result);
19+
}
20+
21+
}

‎src/main/java/chapter4/FindVal.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package chapter4;
2+
3+
/**
4+
* 查找数字
5+
*/
6+
public class FindVal {
7+
8+
static int[] number = {1, 10, 31, 33, 37, 48, 60, 70, 80};
9+
10+
private static int findVal(int val) {
11+
for (int i = 0; i < FindVal.number.length; i++) {
12+
if (val == FindVal.number[i]) {
13+
return i;
14+
}
15+
}
16+
17+
return -1;
18+
}
19+
20+
public static void main(String[] args) {
21+
22+
int val1 = 991;
23+
24+
int val2 = 31;
25+
26+
System.out.println(findVal(val1));
27+
28+
System.out.println(findVal(val2));
29+
}
30+
31+
}

‎src/main/java/chapter4/Shuffle.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chapter4;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* 扑克牌洗牌程序
7+
*/
8+
public class Shuffle {
9+
10+
private static void work() {
11+
String[] kind = {"桃", "砖", "梅", "心"};
12+
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
13+
int[] cardFlag = new int[52];
14+
15+
Random rand = new Random();
16+
for (int i = 0; i < 52; i++) {
17+
int randNum = rand.nextInt(52);
18+
while (cardFlag[randNum] == 1) {
19+
randNum = rand.nextInt(52);
20+
}
21+
cardFlag[randNum] = 1;
22+
23+
System.out.print(kind[randNum / 13] + " ");
24+
System.out.print(numbers[randNum % 13] + " ");
25+
if ((i+1) % 13 == 0) {
26+
System.out.println();
27+
}
28+
}
29+
30+
}
31+
32+
public static void main(String[] args) {
33+
work();
34+
}
35+
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /