核心代码:
/*
循环语句:for循环,while循环,do...while循环。
for循环格式:
for(初始化语句;判断条件语句;控制条件语句) {
循环体语句;
}
执行流程:
A:执行初始化语句
B:执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
C:执行循环体语句;
D:执行控制条件语句
E:回到B继续。
注意事项:
A:判断条件语句无论简单还是复杂结果是boolean类型。
B:循环体语句如果是一条语句,大括号可以省略;如果是多条语句,大括号不能省略。建议永远不要省略。
C:一般来说:有左大括号就没有分号,有分号就没有左大括号
需求:请在控制台输出10次"HelloWorld"
*/
class ForDemo {
public static void main(String[] args) {
//最原始的做法
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");
System.out.println("----------");
//这种做法不好,代码的重复度太高。
//所以呢,我们用循环改进
for(int x=1;x<=10;x++) {
System.out.println("HelloWorld");
}
}
}
//
/*
需求:请在控制台输出数据1-10
*/
class ForDemo2 {
public static void main(String[] args) {
//原始做法
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=1; x<=10; x++) {
System.out.println(x);
}
System.out.println("-------------");
//从0开始
for(int x=0; x<10; x++) {
System.out.println(x+1);
}
}
}
//
/*
需求:求出1-10之间数据之和
分析:
0+1=1
1+2=3
3+3=6
6+4=10
10+5=15
...
由此可见我们要定义两个变量:
一个变量用于存储第一个加数,第一个加数其实保存的是以前的所有数据和。默认初始化值应该是0。
一个变量用于存储第二个加数,第二个加数其实就是每次的数据变化的值。
求和思想。
*/
class ForDemo3 {
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其实是第二个加数
sum = sum + x;
/*
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
...
*/
//sum += x;
}
System.out.println("sum:"+sum);
}
}
//
/*
需求:
A:求1-100之和。
B:求出1-100之间偶数和
C:求出1-100之间奇数和(自己做)
*/
class ForDemo4 {
public static void main(String[] args) {
//求1-100之和。
int sum1 = 0;
for(int x=1; x<=100; x++) {
sum1 +=x;
}
System.out.println("1-100之和是:"+sum1);
System.out.println("------------------");
//求出1-100之间偶数和
//方式1
int sum2 = 0;
for(int x=1; x<=100; x++) {
if(x%2 == 0) {
sum2 += x;
}
}
System.out.println("1-100偶数之和是:"+sum2);
System.out.println("------------------");
//方式2
int sum3 = 0;
for(int x=0; x<=100; x+=2) {
sum3 += x;
}
System.out.println("1-100偶数之和是:"+sum3);
System.out.println("------------------");
}
}
//
/*
需求:求5的阶乘。
什么是阶乘呢?
n! = n*(n-1)! 规则
n! = n*(n-1)*(n-2)*...*3*2*1
求和思想。
求阶乘思想。
*/
class ForDemo5 {
public static void main(String[] args) {
//定义最终结果变量
int jc = 1;
//这里的x其实可以直接从2开始
//for(int x=1; x<=5; x++)
for(int x=2; x<=5; x++) {
jc *=x;
}
System.out.println("1-5的阶乘是:"+jc);
}
}
//
/*
需求:在控制台输出所有的”水仙花数”
分析:
我们都不知道什么叫"水仙花数",你让我怎么做呢?
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
举例:153就是一个水仙花数。
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
A:三位数其实是告诉了我们范围。
B:通过for循环我们就可以实现获取每一个三位数
但是麻烦是如何获取这个三位数的个,十,百位上的数据
我们如何获取一个数据的个,十,百呢?
假设有个一个数据:153
ge: 153%10 = 3
shi: 153/10%10 = 5
bai:153/10/10%10 = 1
qian:x/10/10/10%10
wan: x/10/10/10/10%10
...
C:让ge*ge*ge+shi*shi*shi+bai*bai*bai和该数据比较
如果相同,就把该数据在控制台输出。
*/
class ForDemo6 {
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;
//让ge*ge*ge+shi*shi*shi+bai*bai*bai和该数据比较
if(x == (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
//如果相同,就把该数据在控制台输出。
System.out.println(x);
}
}
}
}
//
/*
练习:
请在控制台输出满足如下条件的五位数
个位等于万位
十位等于千位
个位+十位+千位+万位=百位
分析:
A:五位数就告诉了我们范围。
B:分解每一个五位数的个,十,百,千,万位上的数据
C:按照要求进行判断即可
*/
class ForDemo7 {
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);
}
}
}
}
//
/*
需求:统计”水仙花数”共有多少个
分析:
A:首先必须知道什么是水仙花数
所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
举例:153就是一个水仙花数。
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
B:定义统计变量,初始化值是0
C:三位数告诉了我们范围,用for循环就可以搞定
D:获取每一个三位数的个,十,百的数据
E:按照要求进行判断
F:如果满足要求就计数。
*/
class ForDemo8 {
public static void main(String[] args) {
//定义统计变量,初始化值是0
int count = 0;
//三位数告诉了我们范围,用for循环就可以搞定
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+"个");
}
}
//
/*
需求:请统计1-1000之间同时满足如下条件的数据有多少个:
对3整除余2
对5整除余3
对7整除余2
分析:
A:定义统计变量,初始化值是0
B:1-1000之间是一个范围,用for很容易就可以实现。
C:每个数据要同时满足如下要求
x%3==2
x%5==3
x%7==2
D:如果满足条件,统计数据++即可,最后输出统计变量
*/
class ForDemo9 {
public static void main(String[] args) {
//定义统计变量,初始化值是0
int count = 0;
//1-1000之间是一个范围,用for很容易就可以实现。
for(int x=1; x<=1000; x++) {
/*
每个数据要同时满足如下要求
x%3==2
x%5==3
x%7==2
*/
if(x%3==2 && x%5==3 && x%7==2) {
count++;
System.out.println(x);
}
}
//输出数据
System.out.println("满足这样条件的数据共有:"+count+"个");
}
}
- 邮箱:ithelei@sina.cn
- 技术讨论群:687856230
- GoodLuck