forked from DuGuQiuBai/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b9473b
commit 803bfcb
Showing
51 changed files
with
1,462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
循环语句: | ||
初始化:做一些初始操作 | ||
条件判断:让我们知道要做多少次 | ||
循环体:就是要做的事情 | ||
控制条件变化:通过控制条件,让我们在合适的时候结束 | ||
*/ | ||
class ForDemo { | ||
public static void main(String[] args) { | ||
//在控制台输出一次"HelloWorld" | ||
System.out.println("HelloWorld"); | ||
|
||
//在控制台输出十次"HelloWorld" | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
System.out.println("HelloWorld"); | ||
|
||
//在控制台输出一万次"HelloWorld" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
for循环的格式: | ||
for(初始化语句;判断条件语句;控制条件语句) { | ||
循环体语句; | ||
} | ||
执行流程: | ||
A:首先执行初始化语句 | ||
B:其次执行判断条件语句,看其返回值 | ||
如果是true,就继续 | ||
如果是false,循环结束 | ||
C:执行循环体语句 | ||
D:执行控制条件语句 | ||
E:回到B | ||
*/ | ||
class ForDemo2 { | ||
public static void main(String[] args) { | ||
//在控制台输出10次HelloWorld | ||
for(int x=0; x<10; x++) { | ||
System.out.println("HelloWorld"); | ||
} | ||
System.out.println("--------------"); | ||
|
||
//初始化不从0开始 | ||
for(int x=1; x<=10; x++) { | ||
System.out.println("HelloWorld"); | ||
} | ||
|
||
for(int x=1; x<11; x++) { | ||
System.out.println("HelloWorld"); | ||
} | ||
|
||
for(int x=10; x>0; x--) { | ||
System.out.println("HelloWorld"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
需求:求5的阶乘 | ||
阶乘: | ||
n! = n*(n-1)*(n-2)*...*3*2*1 | ||
n! = n*(n-1)! | ||
*/ | ||
class ForDemo3 { | ||
public static void main(String[] args) { | ||
//定义累乘变量 | ||
int jc = 1; | ||
|
||
for(int x=1; x<=5; x++) { | ||
jc *= x; | ||
} | ||
|
||
System.out.println("5的阶乘是:"+jc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
需求:统计”水仙花数”共有多少个 | ||
分析: | ||
A:我们要统计有多少个满足条件的数据,就要定义一个统计变量 | ||
int count = 0; | ||
B:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。 | ||
C:其各位数字的立方和等于该数本身就是规则 | ||
我们如何取得每一个位上的数据呢? | ||
给了任意的一个数据x 153 | ||
个位:x%10 | ||
十位:x/10%10 | ||
百位:x/10/10%10 | ||
千位:x/10/10/10%10 | ||
... | ||
x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位) | ||
*/ | ||
class ForDemo4 { | ||
public static void main(String[] args) { | ||
//定义统计变量 | ||
int count = 0; | ||
|
||
for(int x=100; x<1000; x++) { | ||
int ge = x%10; | ||
int shi = x/10%10; | ||
int bai = x/10/10%10; | ||
|
||
if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)) { | ||
count++; | ||
} | ||
} | ||
|
||
System.out.println("水仙花数共有:"+count+"个"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class ForTest { | ||
public static void main(String[] args) { | ||
//ÇëÔÚ¿ØÖÆ̨Êä³öÊý¾Ý1-10 | ||
System.out.println(1); | ||
System.out.println(2); | ||
System.out.println(3); | ||
System.out.println(4); | ||
System.out.println(5); | ||
System.out.println(6); | ||
System.out.println(7); | ||
System.out.println(8); | ||
System.out.println(9); | ||
System.out.println(10); | ||
System.out.println("------------"); | ||
|
||
for(int x=0; x<10; x++) { | ||
System.out.println(x+1); | ||
} | ||
System.out.println("------------"); | ||
|
||
for(int x=1; x<=10; x++) { | ||
System.out.println(x); | ||
} | ||
System.out.println("------------"); | ||
|
||
for(int x=10; x>0; x--) { | ||
System.out.println(x); | ||
} | ||
System.out.println("------------"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
需求:求出1-10之间数据之和 | ||
0+1=1 | ||
1+2=3 | ||
3+3=6 | ||
6+4=10 | ||
10+5=15 | ||
... | ||
因为每一次的累加结果都是变化的,所以要定义一个变量,专门用于记录每次累加的结果。 | ||
由于我们需要的1,2,3,4...也是变化的,所以我们也要定义一个变量,而这个变量用循环就能得到每个值。 | ||
*/ | ||
class ForTest2 { | ||
public static void main(String[] args) { | ||
//最好想 | ||
//System.out.println(1+2+3+4+5+6+7+8+9+10); | ||
|
||
//跟循环结合起来 | ||
int sum = 0; | ||
|
||
for(int x=1; x<=10; x++) { | ||
//x=1,2,3,4,...10 | ||
|
||
//sum = sum + x; //sum=0 + 1 = 1; | ||
//sum = sum + x; //sum=1 + 2 = 3; | ||
|
||
//sum = sum + x; | ||
|
||
sum += x; | ||
} | ||
|
||
System.out.println("1-10的和是:"+sum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class ForTest3 { | ||
public static void main(String[] args) { | ||
//求出1-100之间偶数和 | ||
|
||
/* | ||
//定义求和变量 | ||
int sum = 0; | ||
//通过for循环获取每一个数据 | ||
for(int x=1; x<=100; x++) { | ||
//把数据累加 | ||
sum += x; | ||
} | ||
//输出结果 | ||
System.out.println("1-100之和:"+sum); | ||
System.out.println("---------------"); | ||
*/ | ||
|
||
//偶数:能被2整除的数据 | ||
//如何判断数据是否能够被整出呢? x%2 == 0 | ||
|
||
/* | ||
int sum = 0; | ||
for(int x=1; x<=100; x++) { | ||
if(x%2 == 0) { | ||
sum += x; | ||
} | ||
} | ||
System.out.println("1-100的偶数和:"+sum); | ||
*/ | ||
|
||
int sum = 0; | ||
|
||
for(int x=0; x<=100; x+=2) { | ||
sum += x; | ||
} | ||
|
||
System.out.println("1-100的偶数和:"+sum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 | ||
举例:153就是一个水仙花数。 | ||
153 = 1*1*1 + 5*5*5 + 3*3*3 | ||
分析: | ||
A:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。 | ||
B:其各位数字的立方和等于该数本身就是规则 | ||
我们如何取得每一个位上的数据呢? | ||
给了任意的一个数据x 153 | ||
个位:x%10 | ||
十位:x/10%10 | ||
百位:x/10/10%10 | ||
千位:x/10/10/10%10 | ||
... | ||
x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位) | ||
*/ | ||
class ForTest4 { | ||
public static void main(String[] args) { | ||
for(int x=100; x<1000; x++) { | ||
int ge = x%10; | ||
int shi = x/10%10; | ||
int bai = x/10/10%10; | ||
|
||
if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)){ | ||
System.out.println(x); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
需求:请在控制台输出满足如下条件的五位数 | ||
个位等于万位 | ||
十位等于千位 | ||
个位+十位+千位+万位=百位 | ||
分析: | ||
A:五位数告诉我们范围。 | ||
B:获取每一个位上的数据。 | ||
C:满足条件 | ||
个位等于万位 | ||
十位等于千位 | ||
个位+十位+千位+万位=百位 | ||
*/ | ||
class ForTest5 { | ||
public static void main(String[] args) { | ||
for(int x=10000; x<100000; x++) { | ||
int ge = x%10; | ||
int shi = x/10%10; | ||
int bai = x/10/10%10; | ||
int qian = x/10/10/10%10; | ||
int wan = x/10/10/10/10%10; | ||
|
||
if((ge == wan) && (shi == qian) && (ge+shi+qian+wan == bai)) { | ||
System.out.println(x); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
需求:请统计1-1000之间同时满足如下条件的数据有多少个: | ||
对3整除余2 | ||
对5整除余3 | ||
对7整除余2 | ||
分析: | ||
A:定义一个统计变量。 | ||
B:1-1000之间告诉我们了范围,用for循环可以解决 | ||
C:条件 | ||
对3整除余2 | ||
对5整除余3 | ||
对7整除余2 | ||
x%3 == 2 | ||
x%5 == 3 | ||
x%7 == 2 | ||
*/ | ||
class ForTest6 { | ||
public static void main(String[] args) { | ||
//定义一个统计变量。 | ||
int count = 0; | ||
|
||
//1-1000之间告诉我们了范围,用for循环可以解决 | ||
for(int x=1; x<=1000; x++) { | ||
//判断条件 | ||
if(x%3==2 && x%5==3 && x%7==2) { | ||
//System.out.println(x); | ||
count++; | ||
} | ||
} | ||
|
||
System.out.println("共有"+count+"个满足条件的数据"); | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.