Java中的循环
while循环
先循环再判断
public class test{
public static void main(String[]args){
Scanner a=new Scanner(System.in); // 创建扫描器(可输入多个值空格隔开)
System.out.println("请输入你的密码");
while(true){ // <-------- while在这里
int password=a.nextInt(); // 获取扫描器的值(如一次输入多个值再来一个nextInt()获取)
System.out.println("密码不正确请再输入一次");
if(password!=123456){
continue;
}
break;
}
System.out.println("密码正确!");
}
}
eclipse执行结果:
请输入你的密码
123456
密码不正确请再输入一次
密码正确!
常用从Scanner获取数据方法:
public class test {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String aa = a.next(); // 获取字符串类型
int bb = a.nextInt(); // 获取int类型
double cc = a.nextDouble(); // 获取double类型
float dd = a.nextFloat(); // 浮点类型
}
}
do while循环
先执行do再执行while(先判断再循环)
import java.util.Random;
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner a = new Scanner(System.in);
int b;
Random c = new Random(); // 随机模块
int d = c.nextInt(101);
do{
System.out.println("猜猜电脑随机生成的数是多少?");
b = a.nextInt();
if(b < d){
System.out.println("小了");
}else if(b > d){
System.out.println("大了");
}
}while(b != d);
System.out.println("恭喜答对了");
}
}
eclipse执行结果:
猜猜电脑随机生成的数是多少?
1
小了
猜猜电脑随机生成的数是多少?
2
小了
猜猜电脑随机生成的数是多少?
...
for循环
demo
public class test{
public static void main(String[] args){
int sum = 0;
// int i= 1;就是计数器的初始化,只初始化一次
// i <= 100就是循环条件,循环多少次就判断多少次
// i++计数器的累加,循环多少次就累加多少次,执行时机是当前这次循环执行完毕
for(int i = 1; i <= 100; i++){
if(i%2 == 0){
sum += i;
}
}
System.out.println("1~100的偶数和是:" + sum);
}
}
eclipse执行结果:
1~100的偶数和是:2550
demo2
public class test{
/*
打印乘法口诀
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
....
1*9=9 2*9=18..... 9*9=81
*/
public static void main(String[] args){
// 行的循环
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
eclipse执行结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
demo3
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
double sum = 0;
double ave = 0;
for (int i = 1; i <= 5; i++) { // i = 1是申明的变量初始值,i<5循环的条件,i++步长,每次加1
System.out.println("请输入你的第" + i + "门成绩");
int score = a.nextInt();
sum = sum + score;
ave = sum / 5;
}
System.out.println("你的平均成绩是:" + ave);
}
}
eclipse执行结果:
请输入你的第1门成绩
1
请输入你的第2门成绩
2
请输入你的第3门成绩
3
请输入你的第4门成绩
4
请输入你的第5门成绩
5
你的平均成绩是:3.0
for each
import java.util.*;
public class one {
public static void main(String[] args) {
String[] a = {"123","456","789"};
for (String i : a ) { // <--------在这里
System.out.println(i);
}
}
}
执行结果:
123 456 789
break continue
下面的代码展示了break、continue 及循环标签(break和continue可以指定标签继续循环或跳出)
public class test {
public static void main(String[] args) {
int count = 0;
loop1 : while (count <= 10) { // loop1是循环标签可以不写,如果写了名字可随意
loop2 :for (int i = 0;i <= 5;i++) { // 循环5次
if (i == 1) { // 做个演示没什么意义
System.out.println(1);
continue loop2; // 结束本次循环继续下次for循环
}
if (i <= 5) { // 做个演示没什么意义
System.out.println(2);
break loop1; // 指定跳出while
}
}
count += 1;
}
}
}
eclipse执行结果:
2

浙公网安备 33010602011771号