【注意】while语句先判断再执行
while(布尔表达式){
//循环内容
}
package com.ezra.flow;
import java.util.Scanner;
public class WhileDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("从你输入的数,数到100"+"\n"+"请输入数字");
int num;
num = scanner.nextInt();
if (num>=100){
System.out.println("您输入的数已经超过100了!请输入一个小于100的数");
}
//在循环小于100的时候开始数数,循环到num=100的时候,表达式不成立,循环终止
while (num<100){
num++;
System.out.println(num);
}
scanner.close();
}
}
拓展:从1加到你输入的数
package com.ezra.flow;
import java.util.Scanner;
public class WhileDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("从1加到你输入的数"+"\n"+"在这里输入你的数字");
int num01;
int num02 = 0;
int num03 = 0;
num01 = scanner.nextInt();
//num03在小于等于输入的数(num01)的时候,会不断自增
//num02是不断相加的和
//在num03在大于输入的数(num01)的时候,循环结束
//例如:输入的是100,当num03不断自增到101,num03>num01的时候,判定为false,循环结束
while (num03 <=num01 ){
num02 = num02+num03;
num03++;
}
System.out.println("从1加到"+num01+"的和是:"+num02);
scanner.close();
}
}
拓展:用while循环输出1到num之间能被5整除的数,并且每行输出3个
package com.ezra.flow;
import java.util.Scanner;
public class WhileDemo03 {
public static void main(String[] args) {
//用while循环输出1到num之间能被5整除的数,并且每行输出3个
Scanner scanner = new Scanner(System.in);
System.out.print("找出1到num之间所有能被5整除的数"+"\n"+"num=");
int num = scanner.nextInt();
int num01 = 0;
System.out.println("1到"+num+"之间所有能被5整除的数:");
while (num01<=num)
{
num01++;
if (num01%5==0){
System.out.print(num01+"\t");
}
if (num01%(5*3)==0){
System.out.println();
}
}
scanner.close();
}
}
【注意】do…while语句先执行后判断
do{
//循环内容
}while(布尔表达式);
package com.ezra.flow;
public class DoWhileDemo01 {
public static void main(String[] args) {
int num01 = 0;
int num02 = 0;
//区分while和do...while
//do...while先执行,后判读【保证被执行一次】
do {
num02=num02+num01;
num01++;
}while (num01<=100);
System.out.println(num02);
System.out.println("==========");
//while先判断,后执行
while (num01<=100){ //此时num01=101>100,所以下列语句没有运行
num02=num02+num01;
num01++;
}
System.out.println(num02); //此处输出的num02是do...while的运算结果
/*
如果把do...while的语句和while语句交换位置
会发现while输出的num02=5050
do...while输出的num02=5151
因为do...while先执行了一次运算,再发现num01已经大于100
*/
}
}
for(初始化;布尔表达式;更新/迭加){
//代码语句
}
package com.ezra.flow;
import java.util.Scanner;
public class ForDemo01 {
public static void main(String[] args) {
//计算从0到输入的数里所有的奇数和与偶数和
Scanner scanner = new Scanner(System.in);
System.out.println("求从0到num的奇数偶数和"+"\n"+"num=");
int num = scanner.nextInt();
int oddSum =0;
int evenSum =0;
//初始化;布尔表达式;迭加/更新
for (int i = 0; i <= num; i++) {
if (i%2!=0) { //判断i是不是奇数
oddSum += i;
} else { //i不是奇数则为偶数
evenSum+=i;
}
}
System.out.println("0到"+num+"的奇数和为:"+oddSum);
System.out.println("0到"+num+"的偶数和为:"+evenSum);
scanner.close();
}
}
拓展:用for循环输出1到num之间能被5整除的数,并且每行输出3个
package com.ezra.flow;
import java.util.Scanner;
public class ForDemo02 {
public static void main(String[] args) {
//用for循环输出1到num之间能被5整除的数,并且每行输出3个
Scanner scanner = new Scanner(System.in);
System.out.print("找出1到num之间所有能被5整除的数"+"\n"+"num=");
int num = scanner.nextInt();
System.out.println("1到"+num+"之间所有能被5整除的数:");
for (int i = 1; i <= num; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
//System.out.println();
System.out.print("\n"); //两种方法都可以实现换行
}
}
scanner.close();
}
}
拓展:九九乘法表
package com.ezra.flow;
public class ForDemo03 {
public static void main(String[] args) {
//九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
for(声明语句:表达式){
//代码语句
}
package com.ezra.flow;
public class ForDemo04 {
public static void main(String[] args) {
//用两种方式输出数组里的数字
//定义一个数组
int[] number = {10,20,30,40,50};
//for循环
for (int i = 0; i < 5; i++) {
System.out.println(number[i]);
}
System.out.println("=======");
//增强for循环
//遍历数组元素
for(int j:number){
System.out.println(j);
}
}
}
package com.ezra.flow;
public class ForDemo04 {
public static void main(String[] args) {
//用两种方式输出数组里的数字
//定义一个数组
int[] number = {10,20,30,40,50};
//for循环
for (int i = 0; i < 5; i++) {
System.out.println(number[i]);
}
System.out.println("=======");
//增强for循环
//遍历数组元素
for(int j:number){
System.out.println(j);
}
}
}
package com.ezra.flow;
public class BreakDemo01 {
public static void main(String[] args) {
int num = 0;
while (num<100){
num++;
System.out.println(num);
if (num==30)break;
}
}
}
package com.ezra.flow;
public class ContinueDemo01 {
public static void main(String[] args) {
int num = 0;
while (num<100){
num++;
//当num为5的倍数时,跳过此次循环
if (num%5==0){
continue;
}
//当num除5余1的时候,换行(单纯为了输出的时候美观)
if (num%5==1){
System.out.println();
}
System.out.print(num+"\t");
}
}
}
用for语句输出一个三角形:
package com.ezra.flow;
public class TestDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
用for语句输出一个菱形:
package com.ezra.flow;
public class TestDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 6; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 6; i <= 11; i++) {
for (int j = 6; j <= i; j++) {
System.out.print(" ");
}
for (int j = 10; j >= i; j--) {
System.out.print("*");
}
for (int j = 11; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}