Movatterモバイル変換


[0]ホーム

URL:


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

Commit803bfcb

Browse files
committed
Java第四天
1 parent9b9473b commit803bfcb

File tree

51 files changed

+1462
-0
lines changed

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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
循环语句:
3+
初始化:做一些初始操作
4+
条件判断:让我们知道要做多少次
5+
循环体:就是要做的事情
6+
控制条件变化:通过控制条件,让我们在合适的时候结束
7+
*/
8+
classForDemo {
9+
publicstaticvoidmain(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+
}
Lines changed: 37 additions & 0 deletions
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+
classForDemo2 {
17+
publicstaticvoidmain(String[]args) {
18+
//在控制台输出10次HelloWorld
19+
for(intx=0;x<10;x++) {
20+
System.out.println("HelloWorld");
21+
}
22+
System.out.println("--------------");
23+
24+
//初始化不从0开始
25+
for(intx=1;x<=10;x++) {
26+
System.out.println("HelloWorld");
27+
}
28+
29+
for(intx=1;x<11;x++) {
30+
System.out.println("HelloWorld");
31+
}
32+
33+
for(intx=10;x>0;x--) {
34+
System.out.println("HelloWorld");
35+
}
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
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+
classForDemo3 {
10+
publicstaticvoidmain(String[]args) {
11+
//定义累乘变量
12+
intjc =1;
13+
14+
for(intx=1;x<=5;x++) {
15+
jc *=x;
16+
}
17+
18+
System.out.println("5的阶乘是:"+jc);
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
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+
classForDemo4 {
21+
publicstaticvoidmain(String[]args) {
22+
//定义统计变量
23+
intcount =0;
24+
25+
for(intx=100;x<1000;x++) {
26+
intge =x%10;
27+
intshi =x/10%10;
28+
intbai =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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classForTest {
2+
publicstaticvoidmain(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(intx=0;x<10;x++) {
17+
System.out.println(x+1);
18+
}
19+
System.out.println("------------");
20+
21+
for(intx=1;x<=10;x++) {
22+
System.out.println(x);
23+
}
24+
System.out.println("------------");
25+
26+
for(intx=10;x>0;x--) {
27+
System.out.println(x);
28+
}
29+
System.out.println("------------");
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
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+
classForTest2 {
14+
publicstaticvoidmain(String[]args) {
15+
//最好想
16+
//System.out.println(1+2+3+4+5+6+7+8+9+10);
17+
18+
//跟循环结合起来
19+
intsum =0;
20+
21+
for(intx=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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
classForTest3 {
2+
publicstaticvoidmain(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+
intsum =0;
36+
37+
for(intx=0;x<=100;x+=2) {
38+
sum +=x;
39+
}
40+
41+
System.out.println("1-100的偶数和:"+sum);
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
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+
classForTest4 {
21+
publicstaticvoidmain(String[]args) {
22+
for(intx=100;x<1000;x++) {
23+
intge =x%10;
24+
intshi =x/10%10;
25+
intbai =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+
}
Lines changed: 29 additions & 0 deletions
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+
classForTest5 {
16+
publicstaticvoidmain(String[]args) {
17+
for(intx=10000;x<100000;x++) {
18+
intge =x%10;
19+
intshi =x/10%10;
20+
intbai =x/10/10%10;
21+
intqian =x/10/10/10%10;
22+
intwan =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+
}
Lines changed: 35 additions & 0 deletions
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+
classForTest6 {
20+
publicstaticvoidmain(String[]args) {
21+
//定义一个统计变量。
22+
intcount =0;
23+
24+
//1-1000之间告诉我们了范围,用for循环可以解决
25+
for(intx=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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp