java for while循环_Java 基础(for循环,while循环,do-while循环)

程序流程控制:循环结构

循环语句分类:

for 循环

while 循环

do-while 循环

循环语句的四个组成部分

初始化部分(init_statement)

循环条件部分(test_exp)

循环体部分(body_statement)

迭代部分(alter_statement)

58b033f787ad2118873f64ee83f4f4ae.png

for 循环

循环结构的4要素

① 初始化条件

② 循环条件

③ 循环体

④ 迭代条件

for 循环的结构

for(①;②;④){

③;

}

class ForTest{

public static void main(String[] args){

for(int i = 1; i <= 10; i++){

System.out.println("Hello World!");

}

int num = 1;

for(System.out.print('a'); num <= 3; System.out.print('c'), num++){

System.out.print('b');

} // abcbcbc

System.out.println();

// 遍历100以内的偶数,输出所有偶数的和

int sum = 0;

for(int i =1; i <=100; i++){

if (i % 2 ==0){

//System.out.println(i);

sum += i;

}

}

System.out.println(sum); //2550

}

}

求最大公约数和最小公倍数

import java.util.Scanner;

class ForTest2{

public static void main(String[] args){

Scanner scan = new Scanner(System.in);

System.out.println("请输入正整数m");

int m = scan.nextInt();

System.out.println("请输入正整数n");

int n = scan.nextInt();

//求最大公约数

int min = (m <= n)?m: n;

for(int i = min; i >= 1; i--){

if(m % i == 0 && n % i ==0){

System.out.println("最大公约数为: "+ i);

break;

}

}

//求最小公倍数

int max = (m >= n)? m: n;

for(int i = max; i<= m*n; i++){

if(i % n == 0 && i %m ==0){

System.out.println("最小公倍数为: "+ i);

break;

}

}

}

}

while 循环

while 循环的结构

while(②){

③;

④;

}

while 循环不要丢了迭代条件,否则可能导致死循环。

for循环与while循环是可以相互转换的。

for循环与while循环的区别:初始化条件部分的作用范围不同。

class WhileTest{

public static void main(String[] args){

// 遍历100以内所有的偶数

int i = 1;

while(i <= 100){

if(i % 2 == 0){

System.out.println(i);

}

i++;

}

System.out.println(i); //101

}

}

do-while循环

do{

③;

④;

}while(②);

说明:

1.do-while 循环至少会执行一次循环

2.开发中,使用 for 和 while 更多一些,较少使用 do-while

class DoWhileTest{

public static void main(String[] args){

//遍历100以内的偶数

int num = 1;

int sum = 0; //记录总和

int count = 0; //记录个数

do{

if(num % 2 == 0){

System.out.println(num);

sum += num;

count++;

}

num++;

}while(num <= 100);

System.out.println("总和为: " + sum); //2550

System.out.println("个数为: " + count); //50

int number2 = 10;

do{

System.out.println("hello:do-while"); //hello:do-while

number2--;

}while(number2 > 10);

}

}

从键盘输入个数不确定的整数并判断读入的正数和负数的个数,输入为0的时候结束程序

# 注:不限循环条件次数的结构: for(;;) 或 while(true)

# 结束循环的两种方式:1. 循环条件部分返回 false 2.循环体中,执行 break

import java.util.Scanner;

class ForWhileTest{

public static void main(String[] args){

Scanner scan = new Scanner(System.in);

int positiveNumber = 0; //记录正数的个数

int negativeNumber = 0; //记录负数的个数

for(;;){//while(true){

int number = scan.nextInt();

//判断 number 的正负情况

if(number > 0){

positiveNumber ++;

}else if(number < 0){

negativeNumber ++;

}else{

break;

}

}

System.out.println("正数的个数: "+ positiveNumber);

System.out.println("负数的个数: "+ negativeNumber);

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值