for循环
public classTest2 {public static voidmain(String[] args) {//打印0-4的数字
for(int i = 0 ; i < 5 ; i++) { //for(初始化表达式1; 布尔值测试表达式2; 更改表达式4){语句或语句块3;}
System.out.println(i); //初始化只执行一次,初始化后进入布尔值测试表达式,如果条件成立则进入语句或语句块,不成立则不进入会结束循环,进入语句块后,再执行更改表达式4,更改完后重新进入布尔值测试表达式2,就一直重复执行234步骤,直到2不成立就结束循环。
}
}
}
for语句练习:
1、从1循环到150并每行打印一个值,如果是3的倍数在后面加上“foo”,5的倍数就加上"biz",7的倍数就加上“baz".
String result = "";//累加标志
for(int i = 1 ; i<=150 ; i++) {if(i % 3 == 0) {
result= i+" foo";
System.out.println(result);if(i % 5 == 0) {
result+= " biz";
System.out.println(result);if(i % 7 == 0) {
result+= " baz";
System.out.println(result);
}
}
}else if(i % 5 == 0) {
System.out.println(i+" biz");
}else if(i % 7 == 0) {
System.out.println(i+" baz");
}else{
System.out.println(i);
}
}
View Code
代码优化:
public classTest2 {public static voidmain(String[] args) {for(int i = 1 ; i<=150 ; i++) {
String result= "";//累加标志
result += i;//先把i存进累加里面
if(i % 3 == 0) { //这里的i还是int类型
result += " foo";
}if(i % 5 == 0) { //这里为什么不用else if呢,如果是else if那么只会走其中一条路线,如果同时符合3个条件那么else if就不能做到累加了
result += " biz";
}if(i % 7 == 0) {
result+= " baz";
}
System.out.println(result);
}
}
}
View Code
2、打印1~100之间所有的奇数和
public classTest2 {public static voidmain(String[] args) {int result = 0;//累加和
for(int i = 1 ; i < 100 ; i++) {if(i % 2 != 0) { //%取余 一般只有奇数和偶数,i%2==0就是偶数,!=就是奇数
result +=i;
}
}
System.out.println("1~100之间所有奇数的和:"+result);//循环结束后打印结果
}
}
View Code
3、打印1~100之间所有是7的倍数的整数的个数及总和(体会设置计数器的思想)
public classTest2 {public static voidmain(String[] args) {int count = 0;//统计7的倍数的整数的个数
int result = 0;//统计7的倍数的总和
String str = "";//统计7的倍数
for(int i = 1 ; i < 100 ; i++) {if(i % 7 == 0) {
result+=i;
count++;
str+=i+" ";
}
}
System.out.println("7的倍数:"+str+"\n7的倍数个数:"+count+"\n7的倍数总和:"+result);
}
}
View Code
4、输出所有的水仙花数,所谓水仙花数是指一个3位数,其中各位上数字立方和等于其本身。例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
public classTest2 {public static voidmain(String[] args) {int i = 0 , j = 0 , k = 0 , count = 0 ; //i表示百位 j表示十位 k表示个位 count表示:个*个*个 + 十*十*十 + 百*百*百
for(int a = 100 ; a < 1000 ; a++) { //水仙花数是3位数,所以从100开始,999结束
i = (a / 100); //int类型相除会去掉尾数,取百位
j = (a / 10) % 10;///10 先去掉个位数,百位数变成了十位数,然后 %10取余个位数
k = (a % 100) % 10; //a%100可以取到个位数但只对十位数为0的有效,后面加多%10就可以取到个位了,8 % 10 == 8的
count = (i*i*i) + (j*j*j) + (k*k*k);//水仙花数的规律
if(count == a) {//判断当前的值是否为水仙花数
System.out.println(count);
}
}
}
}
View Code
while循环
例子:累加1-100的和
public classTest2 {public static voidmain(String[] args) {//累加1-100的和
int i = 1 , count = 0;//1 初始化i,count为累加变量,只执行一次
while(i<100) {//2 布尔值表达式判断
count+=i; //3 累加
i++; //4 更改循环条件,然后重新进入2判断
}
System.out.println(count);
}
}
View Code
do-while循环
do-while和while的区别,while会先进入布尔值测试表达式,判断是否符合条件才会执行代码,do-while则是先执行一次,再进入布尔值测试表达式。
例子:累加1-100的和
public classTest2 {public static voidmain(String[] args) {//累加1-100的和
int i = 1 , count = 0;//1 初始化i,count为累加变量,只执行一次
do{//2 不进行判断,直接先执行代码
count+=i; //3 累加
i++; //4 更改循环条件,然后重新进入5判断
}while(i<100);//5 布尔值判断,判断后如果成立则进入2,否则结束循环
System.out.println(count);
}
}
View Code
求1-100之间所有偶数的和。分别用for和while语句完成。
for:
public classTest2 {public static voidmain(String[] args) {//累加1-100偶数的和
int count = 0;//1 count为累加变量,只执行一次
for(int i = 1; i < 100 ; i++) {if(i % 2 == 0) {
count+=i;
}
}
System.out.println(count);
}
}
View Code
while:
public classTest2 {public static voidmain(String[] args) {//累加1-100偶数的和
int i = 1 , count = 0;//1 count为累加变量,只执行一次
while(i < 100) {if(i % 2 == 0) {
count+=i;
}
i++;
}
System.out.println(count);
}
}
View Code
嵌套循环
练习:1、九九乘法表
public classTest2 {public static voidmain(String[] args) {//九九乘法表
for(int i = 1 ; i < 10 ; i++) {for(int j = 1 ; j <= i ; j++ ) {//内循环根据外循环循环几次
System.out.print(i +"*"+ j +"="+(i*j)+" ");
}
System.out.println();//内循环结束后换行
}
}
}
View Code
public classTest2 {public static voidmain(String[] args) {//九九乘法表
int i = 1;while(i < 10) {int j = 1;while(j <=i) {
System.out.print(i+"*"+j+"="+(i*j)+" ");
j++;
}
System.out.println();
i++;
}
}
}
View Code
public classTest2 {public static voidmain(String[] args) {//九九乘法表
int i = 1;while(i < 10) {for(int j = 1 ; j <= i ; j++){
System.out.print(i+"*"+j+"="+(i*j)+" ");
}
System.out.println();
i++;
}
}
}
View Code
public classTest2 {public static voidmain(String[] args) {//九九乘法表
for(int i = 1 ; i < 10 ; i++) {int j = 1;while(j <=i) {
System.out.print(i+"*"+j+"="+(i*j)+" ");
j++;
}
System.out.println();
}
}
}
View Code
2、1-100之间的所有质数
public classTest2 {public static voidmain(String[] args) {//1-100之间的所有质数(质数:大于1的自然数,只能被1和本身整除的数)
for(int i = 2,j ; i < 100 ; i++) { //遍历1-100的数 本身:指当前i的值
for(j = 2; j <=i ; j++) { //遍历外层循环的因数,因为除了1和本身可以整除,其他数如果可以整除则不是质数
if(i != j & i % j != 0) { //先判断是该因数是否不等于本身,并且本身对该因数 取余 不为0,如果为0就不是质数了
continue;//遍历不可以整除的因数
}else{if(i == j) {//当上面遍历完因数并且本身等于因数时,结果为质数
System.out.println(i);
}break;//如果本身对该 因数(除了1和本身的因数) 取余为0,就会直接停止当前内循环
}
}
}
}
}
View Code
public classTest2 {public static voidmain(String[] args) {//1-100之间的所有质数(质数:大于1的自然数,只能被1和本身整除的数)
int i = 2;while(i < 100) {//遍历1-100的数
int j = 2;while(j <= i) {//遍历外层循环的因数
if(i != j && i % j != 0) {
j++;continue;
}else{if(i ==j) {
System.out.println(i);
}
j++;break;
}
}
i++;
}
}
}
View Code
public classTest2 {public static voidmain(String[] args) {//1-100之间的所有质数(质数:大于1的自然数,只能被1和本身整除的数)
int i = 2;while(i < 100) {int k = 0; //k是累加取余次数
for(int j = 1 ; j <= i; j++) {if(i % j == 0) {
k++;//质数取余只有2次,1和本身,如果超过两次不是质数
}
}if(k == 2) {
System.out.println(i);
}
i++;
}
}
}
View Code
break语句
continue语句
return语句