Skip to content

Commit 803bfcb

Browse files
committed
Java第四天
1 parent 9b9473b commit 803bfcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1462
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
循环语句:
3+
初始化:做一些初始操作
4+
条件判断:让我们知道要做多少次
5+
循环体:就是要做的事情
6+
控制条件变化:通过控制条件,让我们在合适的时候结束
7+
*/
8+
class ForDemo {
9+
public static void main(String[] args) {
10+
//在控制台输出一次"HelloWorld"
11+
System.out.println("HelloWorld");
12+
13+
//在控制台输出十次"HelloWorld"
14+
System.out.println("HelloWorld");
15+
System.out.println("HelloWorld");
16+
System.out.println("HelloWorld");
17+
System.out.println("HelloWorld");
18+
System.out.println("HelloWorld");
19+
System.out.println("HelloWorld");
20+
System.out.println("HelloWorld");
21+
System.out.println("HelloWorld");
22+
System.out.println("HelloWorld");
23+
System.out.println("HelloWorld");
24+
25+
//在控制台输出一万次"HelloWorld"
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
for循环的格式:
3+
for(初始化语句;判断条件语句;控制条件语句) {
4+
循环体语句;
5+
}
6+
7+
执行流程:
8+
A:首先执行初始化语句
9+
B:其次执行判断条件语句,看其返回值
10+
如果是true,就继续
11+
如果是false,循环结束
12+
C:执行循环体语句
13+
D:执行控制条件语句
14+
E:回到B
15+
*/
16+
class ForDemo2 {
17+
public static void main(String[] args) {
18+
//在控制台输出10次HelloWorld
19+
for(int x=0; x<10; x++) {
20+
System.out.println("HelloWorld");
21+
}
22+
System.out.println("--------------");
23+
24+
//初始化不从0开始
25+
for(int x=1; x<=10; x++) {
26+
System.out.println("HelloWorld");
27+
}
28+
29+
for(int x=1; x<11; x++) {
30+
System.out.println("HelloWorld");
31+
}
32+
33+
for(int x=10; x>0; x--) {
34+
System.out.println("HelloWorld");
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
需求:求5的阶乘
3+
4+
阶乘:
5+
n! = n*(n-1)*(n-2)*...*3*2*1
6+
7+
n! = n*(n-1)!
8+
*/
9+
class ForDemo3 {
10+
public static void main(String[] args) {
11+
//定义累乘变量
12+
int jc = 1;
13+
14+
for(int x=1; x<=5; x++) {
15+
jc *= x;
16+
}
17+
18+
System.out.println("5的阶乘是:"+jc);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
需求:统计”水仙花数”共有多少个
3+
4+
分析:
5+
A:我们要统计有多少个满足条件的数据,就要定义一个统计变量
6+
int count = 0;
7+
B:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。
8+
C:其各位数字的立方和等于该数本身就是规则
9+
我们如何取得每一个位上的数据呢?
10+
11+
给了任意的一个数据x 153
12+
个位:x%10
13+
十位:x/10%10
14+
百位:x/10/10%10
15+
千位:x/10/10/10%10
16+
...
17+
18+
x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位)
19+
*/
20+
class ForDemo4 {
21+
public static void main(String[] args) {
22+
//定义统计变量
23+
int count = 0;
24+
25+
for(int x=100; x<1000; x++) {
26+
int ge = x%10;
27+
int shi = x/10%10;
28+
int bai = x/10/10%10;
29+
30+
if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)) {
31+
count++;
32+
}
33+
}
34+
35+
System.out.println("水仙花数共有:"+count+"个");
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class ForTest {
2+
public static void main(String[] args) {
3+
//ÇëÔÚ¿ØÖÆÌ¨Êä³öÊý¾Ý1-10
4+
System.out.println(1);
5+
System.out.println(2);
6+
System.out.println(3);
7+
System.out.println(4);
8+
System.out.println(5);
9+
System.out.println(6);
10+
System.out.println(7);
11+
System.out.println(8);
12+
System.out.println(9);
13+
System.out.println(10);
14+
System.out.println("------------");
15+
16+
for(int x=0; x<10; x++) {
17+
System.out.println(x+1);
18+
}
19+
System.out.println("------------");
20+
21+
for(int x=1; x<=10; x++) {
22+
System.out.println(x);
23+
}
24+
System.out.println("------------");
25+
26+
for(int x=10; x>0; x--) {
27+
System.out.println(x);
28+
}
29+
System.out.println("------------");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
需求:求出1-10之间数据之和
3+
0+1=1
4+
1+2=3
5+
3+3=6
6+
6+4=10
7+
10+5=15
8+
...
9+
10+
因为每一次的累加结果都是变化的,所以要定义一个变量,专门用于记录每次累加的结果。
11+
由于我们需要的1,2,3,4...也是变化的,所以我们也要定义一个变量,而这个变量用循环就能得到每个值。
12+
*/
13+
class ForTest2 {
14+
public static void main(String[] args) {
15+
//最好想
16+
//System.out.println(1+2+3+4+5+6+7+8+9+10);
17+
18+
//跟循环结合起来
19+
int sum = 0;
20+
21+
for(int x=1; x<=10; x++) {
22+
//x=1,2,3,4,...10
23+
24+
//sum = sum + x; //sum=0 + 1 = 1;
25+
//sum = sum + x; //sum=1 + 2 = 3;
26+
27+
//sum = sum + x;
28+
29+
sum += x;
30+
}
31+
32+
System.out.println("1-10的和是:"+sum);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class ForTest3 {
2+
public static void main(String[] args) {
3+
//求出1-100之间偶数和
4+
5+
/*
6+
//定义求和变量
7+
int sum = 0;
8+
9+
//通过for循环获取每一个数据
10+
for(int x=1; x<=100; x++) {
11+
//把数据累加
12+
sum += x;
13+
}
14+
15+
//输出结果
16+
System.out.println("1-100之和:"+sum);
17+
System.out.println("---------------");
18+
*/
19+
20+
//偶数:能被2整除的数据
21+
//如何判断数据是否能够被整出呢? x%2 == 0
22+
23+
/*
24+
int sum = 0;
25+
26+
for(int x=1; x<=100; x++) {
27+
if(x%2 == 0) {
28+
sum += x;
29+
}
30+
}
31+
32+
System.out.println("1-100的偶数和:"+sum);
33+
*/
34+
35+
int sum = 0;
36+
37+
for(int x=0; x<=100; x+=2) {
38+
sum += x;
39+
}
40+
41+
System.out.println("1-100的偶数和:"+sum);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
3+
举例:153就是一个水仙花数。
4+
153 = 1*1*1 + 5*5*5 + 3*3*3
5+
6+
分析:
7+
A:一个三位数其实告诉我们的是范围,通过for循环就可以搞定。
8+
B:其各位数字的立方和等于该数本身就是规则
9+
我们如何取得每一个位上的数据呢?
10+
11+
给了任意的一个数据x 153
12+
个位:x%10
13+
十位:x/10%10
14+
百位:x/10/10%10
15+
千位:x/10/10/10%10
16+
...
17+
18+
x == (个位*个位*个位 + 十位*十位*十位 + 百位*百位*百位)
19+
*/
20+
class ForTest4 {
21+
public static void main(String[] args) {
22+
for(int x=100; x<1000; x++) {
23+
int ge = x%10;
24+
int shi = x/10%10;
25+
int bai = x/10/10%10;
26+
27+
if(x == (ge*ge*ge + shi*shi*shi + bai*bai*bai)){
28+
System.out.println(x);
29+
}
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
需求:请在控制台输出满足如下条件的五位数
3+
个位等于万位
4+
十位等于千位
5+
个位+十位+千位+万位=百位
6+
7+
分析:
8+
A:五位数告诉我们范围。
9+
B:获取每一个位上的数据。
10+
C:满足条件
11+
个位等于万位
12+
十位等于千位
13+
个位+十位+千位+万位=百位
14+
*/
15+
class ForTest5 {
16+
public static void main(String[] args) {
17+
for(int x=10000; x<100000; x++) {
18+
int ge = x%10;
19+
int shi = x/10%10;
20+
int bai = x/10/10%10;
21+
int qian = x/10/10/10%10;
22+
int wan = x/10/10/10/10%10;
23+
24+
if((ge == wan) && (shi == qian) && (ge+shi+qian+wan == bai)) {
25+
System.out.println(x);
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
需求:请统计1-1000之间同时满足如下条件的数据有多少个:
3+
对3整除余2
4+
对5整除余3
5+
对7整除余2
6+
7+
分析:
8+
A:定义一个统计变量。
9+
B:1-1000之间告诉我们了范围,用for循环可以解决
10+
C:条件
11+
对3整除余2
12+
对5整除余3
13+
对7整除余2
14+
15+
x%3 == 2
16+
x%5 == 3
17+
x%7 == 2
18+
*/
19+
class ForTest6 {
20+
public static void main(String[] args) {
21+
//定义一个统计变量。
22+
int count = 0;
23+
24+
//1-1000之间告诉我们了范围,用for循环可以解决
25+
for(int x=1; x<=1000; x++) {
26+
//判断条件
27+
if(x%3==2 && x%5==3 && x%7==2) {
28+
//System.out.println(x);
29+
count++;
30+
}
31+
}
32+
33+
System.out.println("共有"+count+"个满足条件的数据");
34+
}
35+
}
522 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)