流程控制语句
1.1if分支语句
单分支if语句
...CodeA
if(布尔表达式){
语句组A;
}
...CodeB
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//1.提示用户输入一个年份
System.out.println("请输入一个年份:");
int year = input.nextInt();
//2.判断其是否是闰年
if ((year % 4 == 0 &&year % 100 != 0) || (year % 400 == 0)){
System.out.println(year + "是闰年");
}
System.out.println("判断结束");
}
}
双分支if-else语句
...CdoeA
if(布尔表达式){
语句组A;
}else{
语句组B;
}
...CodeB
判断一个点是否在圆里面
import java.util.Scanner;
public class Test{
public static void main(String[] args){
//数据:点坐标x y 圆心坐标ox oy 点到圆心距离d
//1.输入点坐标
Scanner input = new Scanner(System.in);
System.out.ptntln("输入一个点");
double x = input.nextDouble();
double y= input.nextDouble();
double ox = 0;
double oy = 0;
double r = 10;
//2.计算点到圆心的距离
double d = Math.sqrt(Math.pow(x - ox,2) + Math.pow(y -oy,2));
//3.计算距离与半径的大小关系
if (d > r){
System.out.println("点不在圆里面");
}else{
System.out.println("点在圆里面");
}
System.out.println("判断结束");
}
}
多分支if-else-if语句
...CodeA
if(布尔表达式1){
语句组A;
}else if(布尔表达式2){
语句组B;
}else if(布尔表达式3){
语句组C;
}else{
语句组D;
}
...CodeB
1.2switch分支语句
与if分支语句一样,都是对条件的判断。switch一般用在条件较多的情况下,但是有一个重要的细节,使用if语句时可以对区间或固定值进行判断,switch只能对固定值进行判断
switch (变量){
case 值1: //if(变量 == 值1){语句组A;}
语句组A;
break;
case 值2:
语句组B;
break;
...
case 值n: //if(变量 == 值n){语句组n;}
语句组n;
default:
语句组n+1; //else{语句组n+1;}
}
switch的一些使用细节
- switch所传入的变量:char,byte,short,int,String或者枚举类型
- 值1,值2,一直到n,这几个值必须是同一个数据类型
- 当变量匹配的相关case的值的时候,执行case中的语句,直到遇到break结束;如果该case语句中没有break,则继续向下执行
1.3for循环语句
循环主要解决具有规律性且具有重复性的代码问题,避免程序冗余
循环四要素
- 1.循环初始化:循环的第1次执行从哪里开始
- 2.循环的继续条件:循环从当前轮是否执行下一轮
- 3.循环体:需要被循环执行的部分
- 4.循环的步长、周期:当前循环到下一轮循环之间的变化,控制·循环变化的量
常见的循环问题可以分为两大类:
- 已知循环次数的一般用for语句
- 未知循环次数但已知循环结束条件一般用while语句
for语法格式
for (1;2;4;){
3循环体
}
//执行顺序1-2-3-4-2-3-4-2不满足则结束循环
public class Test{
public static void main(String[] args){
/*
打印*****
*/
for (int i = 1;i <= 5;i++){
System.out.print("*");
}
System.out.println();//换行
System.out.println("=============================");
/*
*****
*****
*****
*****
*/for(int j = 1;j <=4;j++){
for (int i = 1;i <= 5;i++){
System.out.print("*");
}
System.out.println();//换行
}
System.out.println("=============================");
/*
*
**
***
****
*****
******
*******
*/
for(int line = 1;line <= 7;line++){
for(int i =1;i <= line;i++){
System.out.print("*");
}
System.out.println();
}
System.out.println("=============================");
/*
*******
******
*****
****
***
**
*
*/
for(int line = 7;line >=1;line--){
for(int i =1;i <= line;i++){
System.out.print("*");
}
System.out.println();
}
System.out.println("=============================");
/* i j
* 1 1
** 2 12
*** 3 123
**** 4 1234
***** 5 12345
****** 6 123456 j <= i 条件一
***** 7 12345
**** 8 1234
*** 9 123
** 10 12
* 11 1 j <= 12-i 条件二
j <= i && j <= 12-i
*/
for(int i = 1;i <= 11; i++){
for(int j = 1;j <= i && j <= 12-i; j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("=============================");
/* i k(空格的最大数)i-6的绝对值
* 1 -5
** 2 -4
*** 3 -3
**** 4 -2
***** 5 -1
****** 6 0
***** 7 1
**** 8 2
*** 9 3
** 10 4
* 11 5
*/
for(int i = 1;i <= 11; i++){
//打印空格
for(int k = 1;k <= Math.abs(i - 6);k++){
System.out.print(" ");
}
//打印*
for(int j = 1;j <= i && j <= 12-i; j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("=============================");
for(int i = 1;i <= 11; i++){
for(int k = 1;k <= Math.abs(i - 6);k++){
System.out.print(" ");
}
for(int j = 1;j <= i && j <= 12-i; j++){
System.out.print("* ");//打印菱形加空格,撑开上面的图
}
System.out.println();
}
System.out.println("=============================");
/* j
* 1
* * 1 2
* * 1 2 3
* * 1 2 3 4
* * 1 2 3 4 5
* * 1 2 3 4 5 6
* * 1 2 3 4 5
* * 1 2 3 4
* * 1 2 3
* * 1 2
* 1
j == 1 j == i j + i = 12
*/
for(int i = 1;i <= 11; i++){
for(int k = 1;k <= Math.abs(i - 6);k++){
System.out.print(" ");
}
for(int j = 1;j <= i && j <= 12-i; j++){
if(j == 1 || j == i || j+i == 12){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
1.4while循环语句
while主要用于解决循环次数未知,循环结束条件已知的情况
while循环可以和for循环相互转换,因为都逃不开循环四要素
for (int i = 1;i <= 5; i++){
System.out,printkn(i);
}
可以转换为while循环
int = 1;
while(i <= 5){
System.out,printkn(i);
i++;
}
**while的语法格式**
1.循环的初始化
while(2.循环继续的条件){
3.循环体
4.循环的步长、周期
}
数字拆分求和
import java.util.Scanner;
public class Test{
public static void main(String[] args){
//1.输入一个长度未知的数字
Scanner input = new Scanner(System.in);
System.out.println("输入一个数字");
int number = input.nextInt();
int sum = 0;
//2.拆分数字 将每次拆出来的数字进行累加
while (number != 0){
sum += number % 10;
number /= 10;
}
System.out.println(sum);
}
}
猜数字游戏
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//和电脑猜数字游戏,输入1-100
//1.产生一个随机数
/*
Math.random();产生一个[0.1)的随机小数
[0,1) * 101 = [0,101)之间的小数,然后强制类型转换成int类型
*/
int num = (int)(Math.random() * 101);
//2.让用户输入数字,判断是否猜中
Scanner scanner = new Scanner(System.in);
while(true){//先进循环
System.out.print("请输入一个数字:");
int i = scanner.nextInt();
if(i > num){
System.out.println("大了");
}else if(i < num){
System.out.println("小了");
}else {
System.out.println("猜对了");
break;//有true要有break结束,不然会死循环
}
}
}
}
打印九九乘法表
public class Test {
public static void main(String[] args){
/*
while循环打印九九乘法表
i 行 j 列 最大都为9
*/
//循环初始化
int i = 1;
int j = 1;
while(i <= 9){
j = 1;
while (j <= i){
System.out.print(j + "*" + i + "=" + i*j + "\t ");
j++;
}
System.out.println(" ");
i++;
}
//for循环打印
for(int h = 1;h <= 9;h++){
for(int l = 1;l <= h;l++){
System.out.print(l + "*" + h + "=" + l*h + "\t ");//制表符]\t使得打印出来的对齐
}
System.out.println(" ");
}
}
}
1.5break.continue跳转语句
break在循环语句中叫做终止语句,终止是break最近的一层循环
continue在循环语句中叫做跳过语句,跳过本次循环开启下一轮循环