一维数组,5.简单的倍数,但新的编码 [英] 1D Array, multiples of 5. Simple but new to coding
问题描述
编写一个程序,分配和存储被称为数据阵列中的5头20的倍数,没有其他的阵列可以在程序中使用。程序应该输出的数组的元素,根据以下
- 仅写入命令分配和值存储在数据
- 输出阵列:10个/每行的总和线
- 输出阵列:在相反的顺序,5号/与每行的总和线
- 奇数索引的位置值(数据[1],数据[2],数据[5],......),5个值/线和它们的和
- 偶数索引的位置值(数据[3],数据[4],数据[6],...),5个值/线和它们的和
块引用>我有一部分1.到目前为止:
int数据[] =新INT [21]; INT总和= 0;
的for(int i = 1; I< 21;我++){
数据由[i] = 5 * I;
总和+ =数据[I]
如果(I%11 == 0)
的System.out.println();
System.out.print(数据由[i] +);
}
的System.out.println(总和+总和);诠释一个[] =新INT [21];
的for(int i = 1; I< 21;我++){
一个由[i] = 5 * I;
//System.out.print(a[i] +);
}
的System.out.println();我需要休息帮助。
解决方案作为评论说,我们不是在这里code你,<一个href=\"http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems?cb=1\">you将失去学习如何计划非常重要的经验。
不过,我可以给你一些提示,可能你的任务有所帮助。
仅写入命令分配和值存储在数据
输出阵列:10个/每行的总和行
块引用>完成,对不对? ;)
<醇开始=3>
输出阵列:在相反的顺序,5号/与每行的总和线
块引用>我们不会说话,现在在建
阵列功能
或收藏
。结果做吧易:扭转阵列反向循环:的for(int i = 20I&GT; = 0; I - ){
更好地利用数组的长度:
的for(int i = data.length-1; I&GT; = 0; I - ){
为了使增量(例如,通过5),可以使用
D [I] + = 5
等同于D [i] = D [我] + 5
以同样的方式A ++
是A = A + 1
。您需要每行权的5个值?要知道什么时候才能完成一条线,使用的模或余运算符
%
。如果(I%5 == 0)
的这意味着,如果模量I / 5等于0(所以5的倍数),用它来打印总和,新的生产线(
的println
)
<醇开始=4>
奇索引位置值(数据<一个href=\"http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems?cb=1\">1,数据[3],数据[5],......),5个值/线和它们的和
块引用>记住这部分:
I + = 2
比相同的I = I + 2
;)
- 偶数索引的位置值(数据[3],数据[4],数据[6],...),5个值/线和它们的和
块引用>综上所述,创建一个新的变量来存储值 OUTSIDE 的循环,里面的总和:
INT总= 0;
的for(int i = 1; I&LT; 21;我++){
总+ =数据[I] //等于总=总+数据[I]
//更多code
如果(I%5 == 0){
//打印共线的,并跳过线
//重新设置总可变总= 0;
}
//更多code
}Write a program that assigns and stores the first 20 multiples of 5 in the array called Data, no other array can be used in the program. The program should output the elements of the array, according to the following:
- write only the commands to assign and store the values in Data
- output the array: 10 numbers/line with the sum of each line
- output the array: in reversed order, 5 numbers/line with the sum for each line
- the odd indexed position values (Data[1], Data[3], Data[5],...), 5 values/line and their sum
- the even indexed position values (Data[3], Data[4], Data[6],...), 5 values/line and their sum
I have part 1. so far:
int data[]=new int[21]; int sum=0; for(int i=1;i<21;i++){ data[i]=5*i; sum+= data[i]; if(i%11==0) System.out.println(); System.out.print(data[i] + " "); } System.out.println("sum " + sum); int a[]=new int[21]; for(int i=1;i<21;i++){ a[i]=5*i; //System.out.print(a[i] + " "); } System.out.println();
I need help with the rest.
解决方案As told in comments, we're not here to code for you, you will lose really important experience about learning how to program.
But I can give you some tips that may help in your assignment.
write only the commands to assign and store the values in Data
output the array: 10 numbers/line with the sum of each line
DONE, RIGHT? ;)
- output the array: in reversed order, 5 numbers/line with the sum for each line
We wont speak now in built in functions of
Arrays
orCollections
.
Do it easy: to reverse array reverse the loop:for(int i=20i>=0;i--){
Better use the array length:
for(int i=data.length-1;i>=0;i--){
To make increments (for example by 5) you can use
d[i] += 5
equivalent tod[i] = d[i] + 5
in the same waya++
isa = a + 1
.you need 5 values per line right? To know when to finish a line, use modulus or remainder operator
%
.if (i % 5 == 0)
That means, if modulus of i / 5 equals 0 (so 5 multiple), use it to print sum and new line (
println
)
- the odd indexed position values (Data1, Data[3], Data[5],...), 5 values/line and their sum
Remembering for this part:
i+=2
is same thani = i + 2
;)
- the even indexed position values (Data[3], Data[4], Data[6],...), 5 values/line and their sum
To sum, create a new variable to store values OUTSIDE the loop, and sum inside:
int total = 0; for(int i=1;i<21;i++){ total += data[i]; // equals to total = total + data[i] // more code if (i % 5 == 0) { // print total of the line and skip line // reset total variable total = 0; } // more code }
这篇关于一维数组,5.简单的倍数,但新的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!