语法结构


语法结构

语法结构分为(流程控制)
顺序结构

​ 是按照代码的先后顺序加载的

分支结构

单分支if

if(值){
    代码1;
}else{
    代码2;
}

​ if是判断语句,返回的是Boolean(true/false)

多分支switch

​ switch(值){

​ case 值1:

​ 代码1;

​ [break;] (可有可无的)

​ case 值2:

​ 代码2;

​ case 值3:

​ 代码3;

​ default:

​ 代码4;

​ };

​ 只能比较==的

​ 小括号里的值是整点类型的(int short byte char)

​ if的好处(可以写复杂的逻辑)坏处(执行的比较慢)

​ switch好处(判断的效率更高) 坏处(只能做==(固定的))

​ 附加:代码规范

​ 1.可读性(起名字要规范,代码缩进,多加注释)

​ 2.健壮性(程序严谨 )

​ lib提供好的类库,Scanner应用类型

写法:Scanner  scanner = new Scanner(System.in);
	 int i = input.nextInt();

​ 可以在控制面板上面操作。可读取我们输入的文字。 input.nextInt()读取的字符串。

​ 随机数:Math.random();

package com.swy.game;

import java.util.Scanner;

public class Game {
	public static void main(String[] args) {
		//1.随即摇色子的过程  随机产生一个骰子点数  1-6整数
		double value = Math.random();//doubled的取值范围[0.0-1.0)
		int  num = (int)(value*6+1);
		//2.让玩家猜大小
		Scanner input = new Scanner(System.in);
		System.out.println("买定离手");
		String choose = input.nextLine();//帮我们读取输入的文字
		//3.比较点数与猜测结果
		System.out.println("本次摇出的点数为:"+num);
		if ((num <= 3 && choose.equals("小")) || (num >3 && choose.equals("大"))){
			System.out.println("恭喜你 猜对了");
		}else {
			System.out.println("对不起 猜错了");
		}
	}

}

循环结构

​ 重复不停的做同样的事情

​ for while do……while

for循环:

​ 程序中想要执行一个正常的循环 需要满足3个必要条件

​ 1.初始值 2.终点判定条件 3.变化量

​ for( 初始值 ; 终点判定条件 ; 变化量){

​ 好多好多代码;

​ }

package com.swy.game;
//通过循环找寻三位数字的水仙花数;
public class Flowers {
	public static void main(String[] args) {
		for (int num = 100; num < 1000; num++) {
			if (Math.pow(num/100, 3) + Math.pow(num%100/10,3) + Math.pow(num%10,3) == num){
				System.out.println(num+"是水仙花数");
			}
		}
	}
}

循环嵌套

在控制台输出:”* * * *“

​ “* * * *”

​ “* * * *”

​ “* * * *”

package com.swy.game;

public class Star {
	public static void main(String[] args) {
		int count  = 4;
		for (int i = 0; i <= 4; i++) {
			for (int j = 0; j < count; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

}
 

​ 在控制台输出:*

​ **

​ ***

package com.swy.game;

public class StarTow {
	public static void main(String[] args) {
		for (int i = 1; i <= 4; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

}

两个重要的关键字

​ break(终止 终断)

​ cotinue(终断本次循环 执行下一行循环)

两个循环的结构

while循环

​ 初始值;

​ while(终点判断条件){ //()里只能有一个

​ 好多好多执行;

​ 变化量;

​ }

do...while循环

​ 初始值;

​ do{

​ 好多好多执行;

​ 变化量;

​ }while(终点判定条件){

​ }

while先判断后执行 条件不满足不执行

do...while先执行后判断 条件不满足 至少执行一次


package lianxi1;

public class ShuZu1 {
	public static void main(String[] args) {
		//冒泡排序----升序排序
		int[] array = new int[]{5,4,3,2,1};
		//循环
		for (int i = 1; i < array.length; i++) {//控制执行的轮次
			for (int j = array.length-1; j>=i; j--) {//控制比较次数 
				//判断
				if (array[j] < array[j-1]) {
					int max = array[j];
					array[j] = array[j-1];
					array[j-1] = max;
				}
				
			}
		}
		for(int v : array ) {
			System.out.println(v);
		}
	}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM