编程要求
本关的编程任务是补全右侧代码片段中的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);
}
}
编程要求
本关的编程任务是补全右侧代码片段中Begin
至End
中间的代码,具体要求如下:
- 用
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(' ');
}
}
}
}
编程要求
本关的编程任务是补全右侧代码片段中Begin
至End
中间的代码,具体要求如下:
- 用
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(' ');
}
}
}
}