Java循环
Java循环包括while循环、DoWhile循环、For循环。
while循环
while循环当满足条件后才执行循环里面的内容。实例代码如下:
package com.visual.example;
import java.util.Scanner;
public class DoWhileCircle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = (int) (Math.random() * 100 + 1);
// while循环
int Counter = 1;
int guessNumber;
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
while (guessNumber != number) {
Counter += 1;
if (guessNumber > number) {
System.out.println("猜大了");
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
} else if (guessNumber < number) {
System.out.println("猜小了");
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
} else {
System.out.println("恭喜你,猜对了!" + "你猜了" + Counter + "次");
}
}
System.out.println("恭喜你,猜对了!" + "你猜了" + Counter + "次");
}
}
while循环本体部分:
while (guessNumber != number) {
Counter += 1;
if (guessNumber > number) {
System.out.println("猜大了");
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
} else if (guessNumber < number) {
System.out.println("猜小了");
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
} else {
System.out.println("恭喜你,猜对了!" + "你猜了" + Counter + "次");
}
}
DoWhile循环部分
DoWhile循环与while循环不同,DoWhile循环可以先进行循环,然后在循环体结束时判断循环条件,然后再判断循环是否继续进行。实例代码如下:
package com.visual.example;
import java.util.Scanner;
public class DoWhileCircle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = (int) (Math.random() * 100 + 1);
int Counter = 0;
int guessNumber;
do {
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
Counter += 1;
if (guessNumber > number) {
System.out.println("猜大了");
} else if (guessNumber < number) {
System.out.println("猜小了");
} else {
System.out.println("恭喜你,猜对了!" + "你猜了" + Counter + "次");
} // 感觉还不错
} while (guessNumber != number);
}
}
DoWhileCircle循环本体:
do {
System.out.print("请输入一个数字:");
guessNumber = in.nextInt();
Counter += 1;
if (guessNumber > number) {
System.out.println("猜大了");
} else if (guessNumber < number) {
System.out.println("猜小了");
} else {
System.out.println("恭喜你,猜对了!" + "你猜了" + Counter + "次");
} // 感觉还不错
} while (guessNumber != number);
For循环
For循环的格式如下:
for(i=1;i<=n;i++)
for( ; ; )第一段的内容为变量的初始化,第二段内容为每次进入循环本体的判断条件,第三段为每执行一次循环体后对变量的操作。
package com.newHorizon.nineninetable;
public class NinenineMultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t\t");
}
System.out.println();
}
}
}
switch case多重选择语句用法
存在多个结果判断时,用if else判断则会很臃肿,所以这个时候可以用switch case进行判断处理。switch case语句格式为:
switch (choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
//bad input
...
case n:
break;
default:
break;
}
对choice赋值,针对choice不同的值选择不同的case分支,从而快速响应不同的命令情况。代码例子如下:
import java.util.Scanner;
public class SwitchCase {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入对应的数字1-5:");
int choice = in.nextInt();
switch (choice) {
case 1:
System.out.println("20多岁的自己,丝毫不比15岁的自己强");
break;
case 2:
System.out.println("隐约雷鸣 阴霾天空 但盼风雨来 能留你在此");
break;
case 3:
System.out.println("隐约雷鸣 阴霾天空 即使天无雨 我亦留此地");
break;
case 4:
System.out.println("大龙猫");
break;
case 5:
System.out.println("起风了,唯有努力生存");
break;
default:
break;
}
}
}