Java基础(二):控制语句&数组


一、选择结构

1.单分支

if(条件){代码}


public class Test {
    public static void main(String[] args){
        int score = 90;
        if(score>=80){
            System.out.println("成绩达标");
        }
    }
}        

2.双分支

if(条件){代码}else{代码}


//输入成绩,判断是否达标
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int score = input.nextInt();
        if(score>=80){
            System.out.println("成绩达标");
        }else{
            System.out.println("成绩不达标");
        }
    }
}        

3.多分支

if做区间条件
if(条件1){代码1}else if(条件2){代码2}…else{代码N}


//输入成绩,判断获得几等奖
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int score = input.nextInt();
        if(score>=90){
    		System.out.println("特等奖");
	}else if (score>=80){
    		System.out.println("一等奖");
	}else if (score>=75){
    		System.out.println("二等奖");
	}else if (score>=70){
    		System.out.println("三等奖");
	}else{
    		System.out.println("参与奖");
	}
    }
}        

switch做等值条件
switch(算术表达式){case 常量1:代码1;break;…default:代码N;}


//输入年月,输出该月有多少天
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
	int year,month,day=0;
	System.out.println("请输入年份:");
	year = input.nextInt();
	System.out.println("请输入月份:");
	month = input.nextInt();
	System.out.println(year+(year%4==0 ? "是":"非")+"闰年");

	switch(month){
    		case 1: case 3: case 5:case 7:case 8:case 10:case 12:
        		day = 31;
        		break;
    		case 4: case 6: case 9:case 11:
        		day = 30;
        		break;
    		case 2:
        		day = year%4==0 ? 29 :28;
        		break;
}
	System.out.println(MessageFormat.format("{0}年{1}月有{2}天",year,month,day));

4.嵌套分支

if(条件){if(条件){代码}}


//学校组织跑步比赛50米,跑进10秒的进决赛,决赛中再根据性别进行分组
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
	Scanner input = new Scanner(System.in);
    	int score;
	char gender;
	score = input.nextInt();
	gender = input.next().charAt(0);

	if(score<=10){
    	gender = input.next().charAt(0);
    		if(gender != '男' && gender != '女'){
        		System.out.println("性别有误");
    		}else{
        		System.out.println("恭喜你进入"+gender+"子组决赛!");
    		}
	}else{
    		System.out.println("成绩不达标,淘汰");
	}
    }
}

二、循环结构

1.循环结构四要素

  • 循环变量的声明和初始化:初始化表达式
  • 循环条件(变量的作用域/生命周期):布尔表达式
  • 循环变量的更新:更新表达式
  • 循环体:Java语句构成的重复执行代码

2.基础循环

  • 循环次数不确定

while(条件){循环体}

public class Test {
    public static void main(String[] args){
    	byte count = 1;
	while(count<=100){
    	System.out.println("第"+count+"次:我爱你");
    	count++;
	}
    }
}

do{循环体}while(条件)

public class Test {
    public static void main(String[] args){
	byte count = 1;
	do{
    		System.out.println("第"+count+"次:我爱你");
    		count++;
	}while(count<=100);
    }
}
  • 循环次数确定

for(循环变量的声明和初始化;条件;循环变量的更新){循环体}

public class Test {
    public static void main(String[] args){
 	for (byte count = 1;count<=100;count++){
    		System.out.println("第"+count+"次:我爱你");
	}
    }
}
  • 循环跳转语句

continue;(结束本次循环,进入下次循环)
break;(默认情况终止当前循环,进入循环外的下一条语句,也可以指定终止某个循环)

//随机输入一个整数,输出其是否为质数
public class Test {
    public static void main(String[] args){
        Random rand = new Random();
        int num = 1+rand.nextInt(100);
        boolean isPrime = true;
        for(int i = 2;i<=num/2;i++){
            if(num%i==0){
                isPrime = false;
                break;
            }
        }
        System.out.println(num+(isPrime ? "是" : "非")+"质数");
    }
}

3.增强循环

for(xxx v : arr){
v[0 ~ arr.length-1]
}

三、一维数组

1.使用步骤

  • 声明:xxx[] array;
  • 分配空间:array = new xxx[int LENGTH];
  • 赋值:array[int INDEX]
  • 使用:System.out.println(array[int INDEX]);
  • 复合语法(1+2+3)
    xxx[] array = {VALUE1,VALUE2,…}
    xxx[] array = new int[]{VALUE1,VALUE2,…}

2.优缺点

  • 优点
    类型相同:不用考虑类型
    连续:遍历(寻址)
  • 缺点
    类型相同:无法解决不同类型多个值的存储
    连续:插入,移除

3.特性

  • 不可变
  • 长度:int len = array.length;
  • 下标(索引):0 ~ array.length-1
//创建长度为10,取值范围1~100的随机数
//随机生成一个1~100之间的数值
//检索该值是否存在于数组中
public class Test {
    public static void main(String[] args){
    	Random rand = new Random();
	int[] arr = new int[10];
	for (int i = 0; i < arr.length; i++) {
    		arr[i] = rand.nextInt(100)+1;
	}
	int num = rand.nextInt(100)+1;
	boolean has = false;
	for (int i = 0; i < arr.length; i++) {
    		if(num == arr[i]){
        		has = true;
        		break;
    		}
	}
	for (int i = 0; i < arr.length; i++) {
    		System.out.print(arr[i]+"\t");
	}
	System.out.println();
	System.out.println("该数"+num+has ? "存在" : "不存在"+"于此数组中");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值