4.循环结构

一循环
1.定义:反复执行同一个操作内容
2.语法:
a.while(条件){
循环语句
}
b.do{
循环语句
}while(条件);
c.for(表达式1;条件表达式2;表达式3){
循环内容
}
表达式1=循环变量的初始化
表达式2=循环条件
表达式3=循环变量的操作


3.循环的三要素:
a:循环变量的初始化
b:循环条件
c:循环变量的操作

4.三种循环的特点是:
a。while是先判断条件再执行循环体
b。do while 是先执行一次循环体再判断条件,也就是说一定会执行一次
c.for是先判断再执行

5.循环语法的适用场合
a.while:适合循环次数不确定
b.for:适合循环次数确定的场合

6.如何控制循环的流程
a.break:是退出循环,break在哪个循环中就退出哪个循环
b.contiune:结束本次循环的剩余部分,继续下一次循环
c.都要搭配if来使用

二.例子

package com.demo1025;

import java.math.BigDecimal;
import java.util.Scanner;

public class Demo {
	//while案例  打印输出"我喜欢java" 10遍
//	public static void main(String[] args) {
//		int i=1;//循环变量的初始化
//		while(i<=10){//循环条件
//			System.out.println("我喜欢java");
//			i++;//循环变量的操作
//		}
//		System.out.println("完成作业");
//	}

	//练习3 求 1--1000中的偶数和
//	public static void main(String[] args) {
//		int i=0;
//		int sum = 0;//求和的变量
////		while(i<=1000){
////			if(i%2==0){
////				sum += i;//sum = sum+i;
////			}
////			i++;
////		}
//		while(i<=1000){
//			sum += i;
//			i+=2;
//		}
//		System.out.println(sum);
//	}
	
	//练习4 输入一个数:打印输出该数的加法口诀
//		输入:6
//		0+6 = 6
//		1+5 = 6
//		2+4 = 6
//		3+3 = 6
//		4+2 = 6
//		5+1 = 6
//		6+0 = 6
//	public static void main(String[] args) {
//		Scanner sc = new Scanner(System.in);
//		int num = sc.nextInt();
//		int i = 0;
//		int j = num;
////		while(i<=num){
////			System.out.println(i + "+" +(num-i) +"=" +num);
////			i++;
////		}
//		while(i<=num){
//			System.out.println(i + "+" +j +"=" +num);
//			i++;
//			j--;
//		}
//	}
	
	//a:循环变量的初始化  一个字符串 str
	//	b:循环条件    str.equals("y")
	//	c:循环变量的操作   str = sc.next();
	//练习5.系统打印输出“正在提供数据” 然后询问用户是否继续?y/n  当用户输入 y的时候继续提供数据,并且继续询问是否继续,当用户输入n时停止询问  结束程序
//	a:循环变量的初始化  一个字符串 str
//	b:循环条件    str.equals("y")
//	c:循环变量的操作   str = sc.next();
//	public static void main(String[] args) {
//		Scanner sc = new Scanner(System.in);
//		String str = "";
//		do{
//			System.out.println("正在提供数据");
//			System.out.println("是否继续?y/n");
//			str = sc.next();
//		}while(str.equals("y"));
//		System.out.println("程序结束");
//	}
	
	//练习7.请输出如下数列:1  3  5  7  9  11  13  15 输出20位。
//	public static void main(String[] args) {
////		int a=1;
////		for(int i=1;i<=20;i++,a+=2){
////			System.out.print(a+"  ");
////		}
//		for (int i = 1;i<=20;i++) {
//			System.out.print(2*i-1+"  ");
//		}
//	}
	
	//8:输入一个整数,将其倒序输出
//	例如:输入124578   输出875421
//	用循环做
//	public static void main(String[] args) {
//		Scanner sc = new Scanner(System.in);
//		System.out.println("请输入一个整数:");
//		int num = sc.nextInt();
//		while(num!=0){
//			System.out.print(num%10);
//			num/=10;
//		}
//	}
	
	//有如下数列:0  1  1  2  3  5  8  13  21  34  55
	//用程序算出该数列第15位是什么数字?
//	public static void main(String[] args) {
//		int a1=0,a2=1,a3=1;
//		for(int i=1;i<=13;i++){
//			a3 = a1 + a2;
//			a1 = a2;
//			a2 = a3;
//		}
//		System.out.println(a3);
//	}
	
	//break
//	public static void main(String[] args) {
//		int i=0;
//		while(true){
//			System.out.println("我在跑第"+i+"圈");
//			i++;
//			if(i==10){
//				break;//退出循环
//			}
//		}
//	}
	//continue
//	public static void main(String[] args) {
//		for(int i=0;i<5;i++){
//			System.out.println(i);
//			if(i==3){
//				continue;
//			}
//			System.out.println("我是第"+i+"个数");
//		}
//	}
	
	//练习10:计算1到100的偶数和,要求使用continue来实现
	public static void main(String[] args) {
		int sum = 0;
		for(int i=1;i<=100;i++){
			if(i%2==1){
				continue;
			}
			sum += i;
		}
		System.out.println(sum);
	}
}

 

/*
 * 1.循环录入某学生5门课的成绩并计算平均分,如果某分数录入为负,停止录入并提示录入错误  break
 */
package com.class1026;

import java.util.Scanner;

public class Test001 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int score=0;
		double altal=0;
		Scanner sc=new Scanner(System.in);
		for(int i=1;i<=5;i++)
		{
			System.out.println("请输入第"+i+"门课成绩");
			score=sc.nextInt();
			if(score>100||score<0)
			{
				System.out.println("输入错误");
				break;
			}
			altal+=score;
		}
		altal=altal/5;
		System.out.println("平均成绩为:"+altal);
	}

}

  

/*
 * 2.1~10之间的整数相加,得到累加值大于20的当前数   break
 */
package com.class1026;

public class Test002 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int altal=0;
		for(int i=1;i<=10;i++)
		{
			altal+=i;
			if(altal>20)
			{
				System.out.println(i+" "+altal);
				break;
			}
		}
	}

}

  

package com.demo1025;

public class Work1 {
	//1:从100每次递减5输出直至5
//	public static void main(String[] args) {
//		int num = 100;
//		while(num>=5){
//			System.out.println(num);
//			num -= 5;
//		}
//	}
	
	//2.1至50中是7的倍数的数值之和
//	public static void main(String[] args) {
//		int i = 1;
//		int sum = 0;
//		while(i<=50){
//			if(i%7==0){
//				sum = sum+i;
//			}
//			i++;
//		}
//		System.out.println(sum);
//	}
	
	//3:已知操场上有一群人,人数不确定,但是肯定小于100人。
//		现在将这群人按5人一组分,最后会余下4人。
//		按4人一组分,最后会余下3人。
//		按3人一组分,最后会余下2人。
//		请问这群人总共有多少?(编程题)
//	public static void main(String[] args) {
//		int i = 1;
//		while(i<=100){
//			if(i%5==4 && i%4==3 && i%3==2){
//				System.out.println(i);
//			}
//			i++;
//		}
//	}
	
	//4.今有鸡兔同笼,上有三十五头,下有九十四足。问鸡兔各几只
//	public static void main(String[] args) {
//		int ji = 1;
//		while(ji<=35){
//			if(ji*2+(35-ji)*4==94){
//				System.out.println("鸡"+ji+"只,兔子"+(35-ji)+"只");
//			}
//			ji++;
//		}
//	}
	
	//5.2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人?
	public static void main(String[] args) {
		double count = 8;
		int year = 2006;
		while(count<20){
			count = count * (1+0.25);
			year++;
		}
		System.out.println(year);
	}
}

猜你喜欢

转载自www.cnblogs.com/wlxslsb/p/10088070.html
今日推荐