Skip to content

Commit 23327fb

Browse files
author
cicadasmile
committed
Java基础篇:流程控制语句,和算法应用
1 parent 13444a3 commit 23327fb

File tree

11 files changed

+333
-0
lines changed

11 files changed

+333
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* if...else 流程控制
5+
*/
6+
public class Process01 {
7+
public static void main(String[] args) {
8+
// 演示:Node01
9+
if (compare01(40,30)){
10+
System.out.println("40>30:true");
11+
} else {
12+
System.out.println("40>30:false");
13+
}
14+
// 演示:Node02
15+
if (compare01(10,20) && compare01(20,30)){
16+
System.out.println("条件成立");
17+
} else {
18+
System.out.println("条件不成立");
19+
}
20+
// 演示:Node03
21+
if (compare01(20,10) || compare01(20,30)){
22+
System.out.println("条件成立");
23+
} else {
24+
System.out.println("条件不成立");
25+
}
26+
// 演示:Node04
27+
if(compare02(1,1))
28+
if(compare02(2,2))
29+
System.out.println("Running...");
30+
// 演示:Node05
31+
if(compare01(1,2))
32+
if(compare01(5,3)){
33+
System.out.println("5>3");
34+
}
35+
}
36+
37+
private static boolean compare01 (int num1,int num2){
38+
System.out.println("判断:num1="+num1+";num2="+num2);
39+
return num1 > num2 ;
40+
}
41+
private static boolean compare02 (int num1,int num2){
42+
System.out.println("判断:num1="+num1+";num2="+num2);
43+
return num1 == num2 ;
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* else...if 语句
5+
*/
6+
public class Process02 {
7+
public static void main(String[] args) {
8+
elseIf(11) ;
9+
elseIf(9) ;
10+
elseIf(5);
11+
}
12+
13+
private static void elseIf (Integer num){
14+
if (num > 10){
15+
System.out.println("num > 10");
16+
} else if (num > 7){
17+
System.out.println("num > 7");
18+
} else if (num > 4){
19+
System.out.println("num > 4");
20+
} else {
21+
System.out.println("num < 4");
22+
}
23+
}
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.data.type.block03process;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Switch 语句
7+
*/
8+
public class Process03 {
9+
10+
public static void main(String[] args) {
11+
Scanner scan = new Scanner(System.in);
12+
System.out.print("What day is it today:");
13+
String value = scan.next();
14+
weekInfo(value);
15+
}
16+
17+
private static void weekInfo (String value){
18+
switch (value) {
19+
case "Monday":
20+
System.out.println("Monday");
21+
break;
22+
case "Tuesday":
23+
System.out.println("Tuesday");
24+
break;
25+
case "Wednesday":
26+
System.out.println("Wednesday");
27+
break;
28+
case "Thursday":
29+
System.out.println("Thursday");
30+
break;
31+
case "Friday":
32+
System.out.println("Friday");
33+
break;
34+
case "Saturday":
35+
System.out.println("Saturday");
36+
break;
37+
case "Sunday":
38+
System.out.println("Sunday");
39+
break;
40+
default:
41+
System.out.println("Matching failure");
42+
break;
43+
}
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* for 循环
5+
*/
6+
public class Process04 {
7+
public static void main(String[] args) {
8+
// Node01
9+
int sum = 0;
10+
for(int i=1; i<=100; i++) {
11+
sum += i;
12+
}
13+
System.out.println(sum);
14+
15+
// Node02
16+
String[] nameArr = {"Java","C++","C#"} ;
17+
for (String name:nameArr){
18+
System.out.println("name="+name);
19+
}
20+
21+
// Node03
22+
// 输出 i = 13
23+
int i = 0;
24+
for (i++; i++ < 10; i++);
25+
System.out.println(++i);
26+
27+
// 输出:j=3 6 9
28+
int j = 0;
29+
for (j++; j++ < 10; j++){
30+
System.out.println(++j);
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* while 和 do while
5+
*/
6+
public class Process05 {
7+
public static void main(String[] args) {
8+
int num1 = 1;
9+
int num2 = 1;
10+
11+
// while循环
12+
while(num1 <= 3) {
13+
System.out.println("num1 == " + num1);
14+
num1++;
15+
}
16+
17+
// do...while循环
18+
do {
19+
System.out.println("num2 == " + num2);
20+
num2++;
21+
} while(num2 <= 3);
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* Return 语句
5+
*/
6+
public class Process06 {
7+
public static void main(String[] args) {
8+
System.out.println(getNum1());
9+
System.out.println(getNum2());
10+
}
11+
public static int getNum1 (){
12+
int a =100;
13+
try{
14+
return a+1; // 这里是运算逻辑,非赋值
15+
}catch(Exception e){
16+
e.printStackTrace();
17+
}finally{
18+
return a;
19+
}
20+
}
21+
public static int getNum2 (){
22+
int a =100;
23+
try{
24+
return a++; // a++ -> a=a+1 此时a的值改变
25+
}catch(Exception e){
26+
e.printStackTrace();
27+
}finally{
28+
return a;
29+
}
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* break 语句
5+
*/
6+
public class Process07 {
7+
public static void main(String[] args) {
8+
for (int i = 1 ; i < 3 ; i++){
9+
if (i == 2){
10+
break ;
11+
}
12+
System.out.println("i = " + i);
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* Continue 语句
5+
*/
6+
public class Process08 {
7+
public static void main(String[] args) {
8+
for (int i = 1 ; i < 3 ; i++){
9+
if (i == 1){
10+
continue ;
11+
}
12+
System.out.println("i = " + i);
13+
}
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.data.type.block03process;
2+
3+
public class Process09 {
4+
public static void main(String[] args) {
5+
int[] score = {9,8,7,6,5} ;
6+
// 排序次数:最多 length - 1 次
7+
for (int i = 0 ; i < score.length -1 ; i ++){
8+
// 当前排序的集合区间,排序完一个数据就放弃一个
9+
for (int j = 0 ; j < score.length - i - 1 ; j++){
10+
// 冒泡排序:把结果大的向后扔
11+
if (score[j] > score[j+1]){
12+
int temp = score[j] ;
13+
score[j] = score[j+1] ;
14+
score[j+1] = temp ;
15+
}
16+
}
17+
}
18+
// 输出排序后的结果集
19+
for (int i = 0 ; i < score.length ; i++){
20+
System.out.print(score[i]);
21+
}
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.data.type.block03process;
2+
3+
/**
4+
* 排列组合
5+
*/
6+
public class Process10 {
7+
public static void main(String[] args) {
8+
arrange() ;
9+
}
10+
/**
11+
* 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
12+
*/
13+
public static void arrange (){
14+
int i=0; // 百位数
15+
int j=0; // 十位数
16+
int k=0; // 个位数
17+
int t=0; // 计数器
18+
for (i = 1 ; i <= 4 ; i++){
19+
for (j = 1 ; j <= 4 ; j++){
20+
for (k = 1 ; k <=4 ; k++){
21+
if (i != j && j != k && k != i){
22+
t += 1 ;
23+
System.out.print(i*100+j*10+k+"--");
24+
}
25+
}
26+
}
27+
}
28+
System.out.println();
29+
System.out.println("t="+t);
30+
}
31+
}

0 commit comments

Comments
 (0)