2021-03-24课堂笔记

这个博客包含了多个Java编程示例,涵盖了数组的初始化、赋值、求平均分、排序、查找元素以及计算最大值等基本操作。通过这些示例,读者可以深入理解Java中数组的使用和常见计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Main

package edu.xcdq;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	int [] scores = new int[5] ;    // 声明数组,并开辟空间
       int sum = 0 ;
        //赋值
        Scanner scanner = new Scanner(System.in);
        for (int i =0; i<5 ;i++){
            System.out.println("请输入第" +(i+1) + "学生的成绩" );
            scores[i] = scanner.nextInt() ;

                sum += scores[i];  //把数组中i位置元素累加到sum中
        }
        System.out.println("平均分:" + sum/ scores.length);
    }
}


demo01

import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 8:26
 */
public class demo01 {
    public static void main(String[] args) {
        int []scores = {8,4,2,1,23,344,12};
        int sum = 0;
         Scanner scanner = new Scanner(System.in);
         for (int i = 0 ; i<scores.length ;i++){
             System.out.printf( scores[i] +"\t" );
             sum += scores[i];
         }
        System.out.println("数列所有值的和为:" + sum);

         boolean flag = false ;
         int taget = scanner.nextInt();
         for (int a = 0 ; a< scores.length; a++){
             if (scores[a] ==taget){
                 flag=true;
                 System.out.println("包含该元素");
             }
         }
         if (!flag){
             System.out.println("不包含该元素" +
                     "");
         }
    }
}

demo02排序

import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 9:13
 */
public class demo02排序 {
        public static void main(String[] args) {
            int [] scores = new int[5];
            Scanner scanner = new Scanner(System.in);
            for (int i = 0 ;i <scores.length; i++){
                System.out.println("请输入" + (i+1)+ "个数");
                scores[i] = scanner.nextInt();
            }
            //排序
            for (int i = 0 ; i< scores.length; i++){
                System.out.println(scores[i] + "\t");
            }
        }
    }



demo03计算最大值

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 9:19
 */
public class demo03计算大值 {
    //  求最大值    打擂台
    public static void main(String[] args) {
        int [] scores = new int[] {68,45,99,58};
        int max = scores[0]; //默认第一个数组元素为最大值
        for (int i =0 ; i < scores.length;i++){
        if (scores[i] >max){
            max = scores[i];    //如果当前元素的值比max中的值大,用当前值覆盖max中的值
        }
        }
        System.out.println("最大值为" + max);
    }
}

demo04

import java.util.Arrays;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 9:40
 */
public class demo04 {
    public static void main(String[] args) {
        //1 接受用户的输入
        int [] scores = new int[6] ; //在创建的时候就给定了默认值,为0
         Scanner scanner = new Scanner(System.in);
         for (int i = 0 ; i <scores.length-1 ; i++ ){      //留出最后一个空间
             System.out.println("请输入第一个" + (i+1) + "个值");
             scores [i] = scanner.nextInt();
         }
         /*
         for (int i = 0 ; i <scores.length; i++) ;
        System.out.println(scores[i] + "\t");
        }
          */
        //2排序
        Arrays.sort(scores);
        //3 接受用户要插入的值
        System.out.println("请输入你要插入的值");
        int value = scanner.nextInt();
        //4 找到插入位置
        //4.1 找到第一个大于value的位置
        int index = 0 ; //用于记录第一个大于value的位置
        for (int i = 0 ; i<scores.length; i++){
            if (value > scores[i]){
                index = i ;
                break;
            }
        }
        // 4.2 把这个位置及后面的元素向后移,空出一个位置不需要移动
        for (int i = scores.length-2 ;i <=index ; i--){  //最后一个位置不需要移动
            scores[i+1] = scores[i]; // 元素后移
        }

        //4.3   将要插入的元素放在正确位置
        scores[index] = value;

        //5 打印插入后的结果
        for (int i = 0 ;i< scores.length;i++){
            System.out.println(scores[i] + "\t");
        }

    }
}

demo05

import java.util.Arrays;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 10:44
 */
public class demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);        String [] scores = new String[]{"Nike背包,Adidas运动衫,Kappa外套,361°腰包);"};
        System.out.println(scores); //打印数组的起始地址
        double sum = 0;
        System.out.println("请输入会员本月的消费记录");
            for (int i = 0 ; i< scores.length; i++){
                System.out.println(scores[i]);
            }

    }
}

demo06

import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/3/24 11:37
 */
public class demo06 {
    public static void main(String[] args) {
        double [] money = new double[5];
        Scanner scanner = new Scanner(System.in);
        int sum = 0 ;
        for (int i= 0 ; i < money.length; i++){
            System.out.println("请输入第" + (i+1) + "笔金额");
            money[i] =scanner.nextDouble();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值