循环结构:
初始化;
while(循环条件) {
循环语句;
(更新;)
}
先判断,后执行
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int i=1;
while(i<=100){
System.out.println("第"+i+"遍"+"我能行!");
i++;
}
System.out.println("程序结束");
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("练得怎么样?(y/n):");
String str = input.next();
while (!str.equals("y")){
System.out.println("唱歌");
System.out.println("跳舞");
System.out.println("练琴");
System.out.println("练得怎么样?(y/n):");
str =input.next();
}
System.out.println("程序结束");
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("班级人数:");
int count=input.nextInt();
int i=1;
int score,sum=0;
while(i<=count){
System.out.print("第"+i+"位学员成绩:");
score=input.nextInt();
sum +=score;
i++;
}
System.out.print("平均成绩:"+(double)sum/count);
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("年份:");
int i=2006;
int b=80000;
while(b<200000){
b*=1.25;
i++;
}
System.out.print("到"+i+"年,人数增长到"+b);
}
}
do {
循环语句;
(初始化/更新)
}while(循环条件);
先执行,后判断
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String s;
do{
System.out.println("唱歌");
System.out.println("跳舞");
System.out.println("练琴");
System.out.print("可以吗?(y/N)");
s =input.next();
}while (!s.equalsIgnoreCase("y"));
System.out.print("流程结束");
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String name;
do{
System.out.println("学员姓名");
name =input.next();
}while (!name.equalsIgnoreCase("q"));
System.out.print("流程结束");
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
double a;
int b=0,count=1;
System.out.println("\t摄氏度\t华氏温度");
System.out.println("-----------------------------");
do{
a=b*9/5.0+32;
b+=20;
System.out.println("\t"+b+"\t"+a);
}while (count<=10&&b<=250);
System.out.print("流程结束");
}
}
for(初始化;循环条件;更新){
循环语句;
}
break:从机构体中跳出
continue:结束本次循环,进入下一次循环
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
for(int i=1;i<=100;i++){
System.out.print("第"+i+"遍,我能行");
}
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int sum=0,score;
for(int i=1;i<=5;i++){
System.out.print("第"+i+"遍,我能行");
score=input.nextInt();
sum+=score;
}
System.out.print(sum/5.0);
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("num");
int num =input.nextInt();
for(int i=1,j=num;i<=num&&j>=0;i++,j--){
System.out.print(i+"+"+j+"="+(i+j));
}
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("num:");
int num = input.nextInt();
int numBak = num;
int r_digit;
int count=0;
while (num>0){
num/=10;
count++;
}
while(numBak>0){
r_digit=numBak%10;
num+=(int)r_digit*Math.pow(10, count-1);
count--;
numBak/=10;
}
System.out.print(num);
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int sum=0;
for(int i=1;i<=100;i++){
if(i%3!=0){
sum+=i;
}
}
System.out.println(sum);
}
}
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int score,sum=0;
boolean flag=true;
for(int i=1;i<=5;i++){
System.out.println("第"+i+"位学员成绩");
score=input.nextInt();
if(score<0||score>100){
flag=false;
System.out.println("成绩输入错误,程序终止");
break;
}
sum+=score;
}
if(flag){
System.out.println("平均分:"+sum/5.0);
}
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int sum=0;
for(int i=1;i<=10;i++){
sum+=i;
if(sum>20){
System.out.println(i);
break;
}
}
System.out.println(sum);
}
}
package miao;
import java.util.Scanner;
public class Miao {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int sum=0;
for(int i=1;i<=10;i++){
if(i%2==1){
continue;
}
sum+=i;
}
System.out.println(sum);
}
}
总结:
while和for都是先判断后执行
do··while是先执行再判断
break是结束本次循环返还上一次循环
continue是跳过该次判断条件继续执行其他条件