一维数组,5.简单的倍数,但新的编码 [英] 1D Array, multiples of 5. Simple but new to coding

查看:108
本文介绍了一维数组,5.简单的倍数,但新的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

编写一个程序,分配和存储被称为数据阵列中的5头20的倍数,没有其他的阵列可以在程序中使用。程序应该输出的数组的元素,根据以下


  
  

      
  1. 仅写入命令分配和值存储在数据

  2.   
  3. 输出阵列:10个/每行的总和线

  4.   
  5. 输出阵列:在相反的顺序,5号/与每行的总和线

  6.   
  7. 奇数索引的位置值(数据[1],数据[2],数据[5],......),5个值/线和它们的和

  8.   
  9. 偶数索引的位置值(数据[3],数据[4],数据[6],...),5个值/线和它们的和

  10.   

我有一部分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将失去学习如何计划非常重要的经验。

不过,我可以给你一些提示,可能你的任务有所帮助。


  

      
  1. 仅写入命令分配和值存储在数据


  2.   
  3. 输出阵列:10个/每行的总和行


  4.   

完成,对不对? ;)


  <醇开始=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:

    1. write only the commands to assign and store the values in Data
    2. output the array: 10 numbers/line with the sum of each line
    3. output the array: in reversed order, 5 numbers/line with the sum for each line
    4. the odd indexed position values (Data[1], Data[3], Data[5],...), 5 values/line and their sum
    5. 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.

    1. write only the commands to assign and store the values in Data

    2. output the array: 10 numbers/line with the sum of each line

    DONE, RIGHT? ;)

    1. 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 or Collections.
    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 to d[i] = d[i] + 5 in the same way a++ is a = 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)

    1. the odd indexed position values (Data1, Data[3], Data[5],...), 5 values/line and their sum

    Remembering for this part: i+=2 is same than i = i + 2 ;)

    1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆