Java语言之数组

文章提供了三个Java编程任务,涉及读取命令行输入,处理数组,以及去除重复元素并排序。第一个任务要求读取10个整数,计算糖果总数并打印每个数值。第二个任务涉及两个数组的合并与排序。第三个任务需要从输入数组中移除重复元素并按顺序输出。

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

编程要求

本关的编程任务是补全右侧代码片段中的main函数,从而完成分糖果的任务,具体要求如下:

  • candyArray数组保存从命令行输入的10个int型数据。
  • 计算糖果数量的总和,用变量sumCandies存储糖果的总数。
  • 循环输出每个小朋友需要的糖果数量,即遍历candyArray数组输出每个元素(注意:每个元素之间用空格分隔,但结尾不要有空格)。
评测说明

平台将自动编译补全后的代码,并生成若干组测试数据,接着根据程序的输出判断程序是否正确。

以下是测试样例:

测试输入: 1 2 3 4 5 6 7 8 9 10 预期输出: 1 2 3 4 5 6 7 8 9 10[无空格] 55

package step1;

import java.util.Scanner;

public class CandyDelivery {

    public static void main(String[] args) {

        int sumCandies = 0;

        int[] candyArray = new int[10];

        Scanner scanner = new Scanner(System.in); //用于读入测试数据

       

        /********** Begin *********/

        for (int i= 0; i < 10; i ++) {

            candyArray[i] = scanner.nextInt(); //用于读取下一个数据,即每个小朋友想要的糖果

            // 请在此处补全循环代码

                System.out.print(candyArray[i]);

                if(i<9)

                System.out.print(" ");  

            }    

       

        // 请在此处添加遍历数组相关代码

        for (int i=0;i<candyArray.length;i++){

            sumCandies += candyArray[i];

           

        }

           

        /********** End *********/  

               

        System.out.println();

        System.out.print(sumCandies);  

    }

}

编程要求

本关的编程任务是补全右侧代码片段中BeginEnd中间的代码,具体要求如下:

  • class1Grade数组保存第一组数据,用class2Grade保存第二组数据,每一组均是5个int型数据。
  • gradeCount数组保存合并后的数组,并将该数组元素按照从小到大的顺序输出。

评测说明

平台将自动编译补全后的代码,并生成若干组测试数据,接着根据程序的输出判断程序是否正确。

以下是测试样例:

测试输入: 1 3 5 7 9 0 2 4 6 8 预期输出: 0 1 2 3 4 5 6 7 8 9

package step2;

import java.util.Scanner;

import java.util.Arrays;

public class MarkStatistics {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int[] class1Grade = new int[5];

        int[] class2Grade = new int[5];

        int[] gradeCount = new int[class1Grade.length + class2Grade.length];

        /********** Begin *********/

        for(int i=0;i<2;i++){

            for(int j=0;j<5;j++){

                int num = scanner.nextInt();

                if(i==0){

                    class1Grade[j]=num;

                }

                else{

                    class2Grade[j]=num;

                }

            }

        }

        System.arraycopy(class1Grade,0,gradeCount,0,class1Grade.length);

        System.arraycopy(class2Grade,0,gradeCount,class1Grade.length,class2Grade.length);

        Arrays.sort(gradeCount);

       

       

        /********** End *********/

        for (int i = 0; i < gradeCount.length; i++) {

            System.out.print(gradeCount[i]);

            if (i != gradeCount.length - 1) {

                System.out.print(' ');

            }

        }

    }

}

编程要求

本关的编程任务是补全右侧代码片段中BeginEnd中间的代码,具体要求如下:

  • record数组保存从命令行输入的10个int型数据。
  • recordRecovery数组保存去除重复数据后的元素。示例:数组1 1 2 2 3 3中删去重复的数字,即转变为1 2 3
  • 按照数字大小顺序,循环输出recordRecovery数组。
测试说明

平台将自动编译补全后的代码,并生成若干组测试数据,接着根据程序的输出判断程序是否正确。

以下是测试样例:

测试输入: 1 2 2 3 9 9 5 5 8 8 预期输出: 1 2 3 5 8 9

package step3;

import java.util.Arrays;

import java.util.Scanner;

public class RecordRecovery {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int[] record = new int[10];

        int[] recordRecovery = new int[10];

        /********** Begin *********/

        /*for(int i=0;i<5;i++){

            class1Grade[i]=scanner.nextInt();

        }

        for(int i=0;i<5;i++){

            class2Grade[i]=scanner.nextInt();

           // class2Grade[i]=scanner.nextInt();

        }

        int[] class3=new int[class1Grade.length+class2Grade.length];

        //int[] class3=new int[class1Grade.length+class2Grade.length];

        System.arraycopy(class1Grade,0,class3,0,class1Grade.length);

        System.arraycopy(class2Grade,0,class3,5,class2Grade.length);

        Arrays.sort(class3);

        for(int i=0;i<10;i++){

            System.out.print(class3[i]+" ");

        }

*/

         for(int i = 0; i < 10 ; i++ ){

        if(scanner.hasNextInt()){

            record[i] = scanner.nextInt();

        }

    }

    //先进行排序

    Arrays.sort(record);

    recordRecovery[0] = record[0];

    //将不同的项放入新的数组

    int j = 0;

    for(int i = 1; i < record.length; i++){

        if(record[i] != recordRecovery[j]){

            j++;

            recordRecovery[j]=record[i];

        }

    }

    //Arrays.sort(recordRecovery);

    //这个j就是实际上有多少个真实的数据

    int num = j+1;

       

        /********** End *********/

        for (int i = 0; i < num; i++) {

            System.out.print(recordRecovery[i]);

            if (i != num - 1) {

                System.out.print(' ');

            }

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值