循环结构介绍:for; while; do-while
for(1初始化条件 ; 2循环条件(必须是boolean) ; 4迭代部分 ){
3循环体部分 执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - 2 - 3 - 4 - 2 - 3 - 4 - 2 - 3 - 4 。。。。(直到循环结束)
}
注意:跳出for循环用break;!
题目:输出一百以内的奇数,并且计算他们的和,并且计算一共有多少个奇数:
public class for01 {
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月16日上午9:01:04
*/
public static void main(String[] args) {
int nums=0;
int cout=0;
for(int i=1;i<=100;++i){
if(i%2==1){
System.out.println(i);
nums+=i;
++cout;
}
}
System.out.println(nums);//总和
System.out.println(cout);//个数
}
}
import java.util.Scanner;
public class for03 {
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月16日上午9:01:04
*/
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("请输入第一个正整数:");
int m=scan.nextInt();
System.out.println("请输入第二个正整数:");
int n=scan.nextInt();
int min=(m>n)?n:m;
int max=(m>n)?m:n;
for(int i=min;i<=min;--i){
if(m%i==0&&n%i==0){
System.out.println("最大公约数:"+i);
break;
}
}
for(int i=max;i<=(max*min);++i){
if(i%m==0&&i%n==0){
System.out.println("最小公倍数:"+i);
break;
}
}
}
}
题目:输出从1~150,每行输出一个数。如果是3的倍数就在后面加上foo;如果是5的倍数,就在后面加上biz;如果是7的倍数,就在后面加上baz。
public class for02 {
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月16日上午9:01:04
*/
public static void main(String[] args) {
for(int i =1;i<=150;++i){
System.out.print(i+"\t");
// 是三的倍数就加 foo,是五的倍数就加biz, 是七的倍数就加baz
if(i%3==0){
System.out.print("foo"+"\t"); }
if (i%5==0){
System.out.print("biz"+"\t"); }
if(i%7==0) {
System.out.print("baz"+"\t");}
System.out.println();}
}
}
public class for04 {
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月16日上午9:01:04
*/
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int h = i / 100;
int t = (i / 10) % 10;
int o = i % 10;
if (h * h * h + t * t * t + o * o * o == i) {
System.out.println(i+"="+h+"*"+h+"*"+h+"+"+t+"*"+t+"*"+t+"+"+o+"*"+o+"*"+o);}
// for (int i=100;i<1000;i++) {
// if (((i/100)*(i/100)*(i/100))+((i/10)*(i/10)*(i/10))+((i%10)*(i%10)*(i%10))==i) {
// System.out.println(i);}
}
}}
while循环结构
1.初始条件;
while(2.循环条件){
3.循环体;
4.迭代;}
注意:while循环一定不能丢4.迭代;如果丢了,可能就是死循环。执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - 2 - 3 - 4......; for循环和while 循环可以互相转换。
区别:for循环和while循环初始化条件作用范围不一样。
题目:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入0时程序结束:
//题目:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入0时程序结束:
import java.util.Scanner;
public class while01 {
/**
*
* @Desciption
* @author Abraham
* @email 1290807550@qq.com
* @version JDK1.8
* @date 2021年3月16日上午9:01:04
*/
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请开始输入整数:");
System.out.println("注意:当输入‘0’时程序结束");
int psnum,nanum;
psnum=0;nanum=0;
while(true) {
int num=scan.nextInt();
if(num>0){psnum++;}
else if(num<0){nanum++;}
else break;
}
System.out.println("正整数个数为:"+psnum);
System.out.println("负整数个数为:"+nanum);
}
}
do-while循环
1.初始条件;
do{
3.循环体;
4.迭代;
}while(2.循环条件);
执行过程:1 - 3 - 4 - 2 - 3 - 4 - 2 - 3 - 4 -2......
说明:do-while循环最少循环一次循环体(体会一下)
开发中使用for和while要绝对多一些,较少使用do-while。