新职课(chapter2)

跳出循环

wai:for(int i=0; i<8; i++){
    for (int j=0; j<8; j++){
        System.out.print("i="+i);
        System.out.println("j="+j);
        if (i==5){
			// break;  // 跳出整个内层循环
            break wai; // 跳出整个循环
            continue;	// 跳出内层本轮循环
        }
    }
}

判断用户输入

import java.util.Scanner;

Scanner input = new Scanner(System.in);
System.out.println("输入一个数字:");
// 判断输入类型
if(input.hasNextInt()){ // int型
    int num = input.nextInt();
    System.out.println(num);
}else{
    System.out.println("不是数字");
}
  • 今天是今年的第几天?
public static void main(String[] args) {
    int year = 0;
    int month = 0;
    int day = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("请输入年份:");
    if(input.hasNextInt()){
        year = input.nextInt();
    }
    System.out.println("请输入月份:");
    if(input.hasNextInt()){
        month = input.nextInt();
    }
    System.out.println("请输入日:");
    if(input.hasNextInt()){
        day = input.nextInt();
    }

    int sum = 0;
    for(int i=1; i<month; i++){
        switch (i){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                sum += 31;
                break;  // 跳出switch
            case 4:
            case 6:
            case 9:
            case 11:
                sum += 30;
                break;
            case 2: // 四年一润,百年不润,四百年再润
                if(year%4==0 && year%100!=0 || year%400==0){
                    sum += 29;
                }else {
                    sum += 28;
                }
                break;
        }
    }
    sum += day;
    System.out.println("今年的第"+sum+"天");
}

求数组的最大值

public static void main(String[] args) {
    int[] arr1 = {1,2,3,45,36,5,9,7,18,2,0,4};
    int[] arr2 = new int[10];   // 可以装十个
    // 找数组的最大值
    int max = arr1[0];
    for (int i=1; i< arr1.length; i++){
//            if(arr1[i]>max){
//                max = arr1[i];
//            }
        max = arr1[i]>max?arr1[i]:max;
    }
    System.out.println(max);
}
  • 冒泡排序:只需两层for循环
public static void main(String[] args) {
// 每次都有一个元素能确定最终位置
    int[] arr = {1,6,7,9,14,25,13,14,88,7,9,6,8};
    // 外层循环 length-1
    for (int i=0; i<arr.length-1; i++){
        // 内层循环 length-i-1
        for (int j=0; j<arr.length-i-1; j++){
            if(arr[j]>arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    for (int i=0; i<arr.length; i++){
        System.out.println(arr[i]);
    }
}
  • 二分查找:更新三个索引即可
public static void main(String[] args) {
    int[] arr = {1,6,6,7,7,8,9,9,13,14,14,25,88};   // 必须有序
    int num = 19;   // 要查找的数字
    int minIndex = 0;
    int centreIndex = 0;
    int maxIndex = arr.length-1;

    centreIndex = (maxIndex+minIndex)/2;
    while(true) {
        if (arr[centreIndex]>num){
            maxIndex = centreIndex - 1;
        }else if (arr[centreIndex]<num){
            minIndex = centreIndex + 1;
        }else {
            break;  // 找到了,直接返回索引
        }

        if (minIndex>maxIndex){ // 等于没关系,三个索引重合而已
            centreIndex = -1;
            break;
        }
        centreIndex = (maxIndex+minIndex)/2;    // 更新索引
    }
    System.out.println(centreIndex);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Roy_Allen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值