Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 1D阵列,5的倍数。简单但新的编码_Java_Arrays - Fatal编程技术网

Java 1D阵列,5的倍数。简单但新的编码

Java 1D阵列,5的倍数。简单但新的编码,java,arrays,Java,Arrays,编写一个程序,分配并存储数组中5的前20个倍数,称为Data,程序中不能使用其他数组。程序应根据以下要求输出阵列的元素: 仅编写命令以在数据中分配和存储值 输出数组:10个数字/行,每行的总和 输出数组:按相反顺序,5个数字/行,每行的和 奇数索引位置值(数据[1]、数据[3]、数据[5]、…)、5个值/行及其和 偶数索引位置值(数据[3]、数据[4]、数据[6]、…)、5个值/行及其总和 我有第一部分。到目前为止: int data[]=new int[21]; int sum=0;

编写一个程序,分配并存储数组中5的前20个倍数,称为Data,程序中不能使用其他数组。程序应根据以下要求输出阵列的元素:

  • 仅编写命令以在数据中分配和存储值
  • 输出数组:10个数字/行,每行的总和
  • 输出数组:按相反顺序,5个数字/行,每行的和
  • 奇数索引位置值(数据[1]、数据[3]、数据[5]、…)、5个值/行及其和
  • 偶数索引位置值(数据[3]、数据[4]、数据[6]、…)、5个值/行及其总和
  • 我有第一部分。到目前为止:

    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();
    
    int data[]=newint[21];整数和=0;
    
    对于(inti=1;i,如注释中所述,我们不是来为您编写代码的

    但我可以给你一些提示,也许对你的作业有帮助

  • 仅编写命令以在数据中分配和存储值

  • 输出数组:10个数字/行,每行的总和

  • 完成了,对吗?;)

  • 输出数组:按相反顺序,5个数字/行,每行的和
  • 我们现在不会谈论
    数组
    集合的内置函数

    简单一点:要反转数组,请反转循环:

    for(int i=20i>=0;i--){
    
    更好地使用数组长度:

    for(int i=data.length-1;i>=0;i--){
    

    要进行增量(例如5),可以使用
    d[i]+=5
    等效于
    d[i]=d[i]+5
    ,方法与
    a++
    is
    a=a+1
    相同

    每行需要5个值,对吗?要知道何时完成一行,请使用

    也就是说,如果i/5的模等于0(所以是5的倍数),则使用它打印总和和新行(
    println

  • 奇数索引位置值(数据,数据[3],数据[5],…),5个值/行及其和
  • 记住这部分内容
    i+=2
    i=i+2
    相同)

  • 偶数索引位置值(数据[3]、数据[4]、数据[6]、…)、5个值/行及其总和
  • 要求和,请创建一个新变量来存储循环外部的值,然后在循环内部求和:

    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
     }
    
    int-total=0;
    
    对于(int i=1;我会遇到堆栈溢出!人们通常很乐意帮助调试,但不会为您编写代码。请尝试自己动手,然后带着一个特定问题的解决方案回来。您已接近第1部分,但还有一点距离。您的
    数据
    数组有21个元素,但只需要20个元素。请记住,Java数组是零索引的;也就是说,第一个索引是0,第二个是1,第三个是2,等等…最后一个索引是n-1(其中n是数组的大小).帮助?你是什么意思?可能是建议?见解?还是做我的家庭作业?这对我来说就像是编写循环的练习。提示:除了
    ++
    ,还有其他方法可以增加循环计数器,例如
    +=5
    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
     }