1 顺序结构
前面提到的短路运算会出现跳过部分代码的情况,但在正常情况下,没有代码影响程序运行时,会按照从上至下的顺序进行。
2 if选择结构
2.1 if单选择结构
package com.ifJieGou;
import java.util.Scanner;
public class ifdemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数:");
if (scanner.hasNextInt()){
int num = scanner.nextInt();
System.out.println("它的三次方为:"+(num*num*num));
}
scanner.close();
}
}
if单选择是判断是否满足条件,当满足条件时,将运行if后的代码,当不满足时,将跳过后面的代码。
2.2 if双选择
package com.ifJieGou;
import java.util.Scanner;
public class ifdemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数:");
if (scanner.hasNextInt()){
int num = scanner.nextInt();
System.out.println("它的三次方为:"+(num*num*num));
}else {
System.out.println("错误!");
}
scanner.close();
}
}
if双选择也是判断是否满足条件,当满足条件,将运行if后else前的代码,跳过else后的代码;当不满足,将跳过if后代码,运行else后的代码。
2.3 if多选择
package com.ifJieGou;
import java.util.Scanner;
public class ifdemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字:");
if (scanner.hasNextInt()){
int i = scanner.nextInt();
System.out.println("它的三次方为:"+(i*i*i));
}else if (scanner.hasNextFloat()) {
float f = scanner.nextFloat();
System.out.println("它的三次方为:"+(f*f*f));
}else {
System.out.println("错误!");
}
scanner.close();
}
}
if多选择,是由多个判断组成的选择顺序结构,输入的数据会被从上到下检测是否满足某个条件,当满足时,就会进入他后方的代码进行运行。它是从上到下检测的,所以当某个值满足两个条件时,会优先进入在前的选择进行运行,而在后的选择将不运行。
package com.ifJieGou;
import java.util.Scanner;
public class ifdemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数数字:");
int num = scanner.nextInt();
if (num>=10){
System.out.println("它的三次方为:"+(num*num*num));
}else if (num<=10) {
System.out.println("它的二次方为:"+(num *num));
}else {
System.out.println("错误!");
}
scanner.close();
}
}
输入:10
输出:1000
2.4 if嵌套结构
package com.ifJieGou;
import java.util.Scanner;
public class ifdemo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
if (num<100){
num++;
if (num>=50){
System.out.println(num);
}
}
}
}
在if中嵌套if使得可以同时利用多个条件进行筛选。
3 switch选择结构
package com.switchJG;
import java.util.Scanner;
public class swdemo01 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int goal = scanner.nextInt();
switch (goal){
case 0:
System.out.println("很遗憾,0分");
break;
case 1:
System.out.println("不及格");
break;
case 2:
System.out.println("及格");
break;
case 3:
System.out.println("优秀");
break;
default:
System.out.println("未知");
}
}
}
switch会将变量与case的值进行比较,当值相等时,会运行后面的代码。
- switch选择结构可以对字符串进行判断。在源码层面是将字符串转换成hash值进行比较。
- case穿透,注意如果case后没有break,会出现case穿透,即在达成一个case条件后,将后方的代码全部运行。
4 while循环结构
4.1代码结构
package com.XunHuan;
public class whdemo01 {
public static void main(String[] args) {
int num = 0;
while (num<10) {
System.out.println(num++);
}
}
}
- while循环,只要判断条件的值为true,将会一直循环运行,直到值为false为止。
- 一般要设立一个会停止的循环,让程序能够停止。除了一些需要一直运行的死循环while(true)。
4.2 例子
- 计算1+2+3+…+100。
package com.XunHuan;
public class whdemo02 {
public static void main(String[] args) {
int num1 = 1;
int num2 = 0;
while (num1<=100){
num2+=num1;
num1++;
}
System.out.println(num2);
}
}
- 我们都知道高斯的等差数列求和方式,但在计算机中,通过自己相加的代码反而更加简单。
do…while循环
- while和do…while 的主要区别在于,do…while是先执行再判断是否进入循环,while是先判断再执行,因此do…while循环保证至少执行一次。
public class dowhdemo01 {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
do {
num1++;
num2+=num1;
}while(num1<100);
System.out.println(num2);
}
}
public class dowhdemo02 {
public static void main(String[] args) {
int i = 0;
while (i<0){
System.out.println("while");
}
do {
System.out.println("do");
}while (i<0);
}
}
- 如果while进入循环,那么结果将输出while,Dowhile进入循环将输出do。可以发现仅仅只输出了do,表明while未进入循环。
5 For循环
- for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
5.1 代码格式
public class fordemo01 {
public static void main(String[] args) {
for (int i =1;i<=100;i++){
System.out.println(i);
}
}
}
5.2 例子
- 分别计算0到100的奇数和与偶数和
public class fordemo02 {
public static void main(String[] args) {
int odd = 0;
int even = 0;
for (int i = 0; i <=100; i++) {
if (i%2 == 0){
even+=i;
}else{
odd+=i;
}
}
System.out.println(odd);
System.out.println(even);
}
}
- 用while和for循环输出1~1000之间能被5整除的数,并且每行输出3个
public class fordemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i+"\t");//print输出完不会换行
}
if (i%15 == 0){
System.out.println();//print输出完会换行
}
}
}
}
public class whdemo03 {
public static void main(String[] args) {
int i = 0;
while (i<=1000){
i++;
if (i%5 == 0){
System.out.print(i+"\t");
}
if (i%15 == 0){
System.out.println();
}
}
}
}
- 打印九九乘法表
public class fordemo04 {
public static void main(String[] args) {
for (int a = 1; a < 10; a++) {
System.out.println();
for (int b = 1; b <=a; b++) {
System.out.print(b+"*"+a+"\t");
}
}
}
}
1*1
1*2 2*2
1*3 2*3 3*3
1*4 2*4 3*4 4*4
1*5 2*5 3*5 4*5 5*5
1*6 2*6 3*6 4*6 5*6 6*6
1*7 2*7 3*7 4*7 5*7 6*7 7*7
1*8 2*8 3*8 4*8 5*8 6*8 7*8 8*8
1*9 2*9 3*9 4*9 5*9 6*9 7*9 8*9 9*9
可以发现,for循环也可以嵌套。