Java笔记-控制结构(do...while)

do while 简介

在这里插入图片描述
请添加图片描述

do while 细节

在这里插入图片描述

public class DoWhileExercise01 { 

	//编写一个main方法
	public static void main(String[] args) {
		//统计1---200之间能被5整除但不能被3整除的 个数
		//化繁为简
		//(1) 使用do-while输出 1-200
		//(2) 过滤 能被5整除但不能被3整除的数 %
		//(3) 统计满足条件的个数 int count = 0; 
		//先死后活
		//(1) 范围的值 1-200 你可以做出变量
		//(2) 能被5整除但不能被3整除的 , 5 和 3 可以改成变量 
		int i = 1;
		int count = 0; //统计满足条件的个数
		do {
			if( i % 5 == 0 && i % 3 != 0 ) {
				System.out.println("i=" + i);
				count++;
			}
			i++;
		}while(i <= 200);

		System.out.println("count=" + count);
	}
}
import java.util.Scanner;
public class DoWhileExercise02 { 

	//编写一个main方法
	public static void main(String[] args) {

		//如果李三不还钱,则老韩将一直使出五连鞭,直到李三说还钱为
		//[System.out.println("老韩问:还钱吗?y/n")]   do...while ..
		//
		//化繁为简
		//(1) 不停的问还钱吗?
		//(2) 使用char answer 接收回答, 定义一个Scanner对象
		//(3) 在do-while 的while 判断如果是 y 就不在循环
		//一定自己动脑筋..
		Scanner myScanner  = new Scanner(System.in);
		char answer = ' ';
		do {
			System.out.println("老韩使出五连鞭~");
			System.out.println("老韩问:还钱吗?y/n");
			answer = myScanner.next().charAt(0);
			System.out.println("他的回答是" + answer);
		}while(answer != 'y');//判断条件很关键

		System.out.println("李三还钱了");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值