Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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中索引变量的定义_Java_Arrays - Fatal编程技术网

java中索引变量的定义

java中索引变量的定义,java,arrays,Java,Arrays,我对Java非常陌生,目前正在学习数组。我们这周的作业是 编写一个程序,声明一个包含50个“double”类型元素的数组“alpha”。初始化数组,使前25个元素等于索引变量的平方,后25个元素等于索引变量的三倍 我的问题是。是索引位置的元素中被视为索引变量的值。例如,如果alpha[2]=3将是索引变量,在读取赋值时,我将取3的平方 我的另一个想法是我必须将索引编号[0]、[1]、[2]平方 谢谢你的任何意见,如果这是在错误的领域,我道歉 感谢您迄今为止的投入。我想说的是“索引变量”到底是什

我对Java非常陌生,目前正在学习数组。我们这周的作业是

编写一个程序,声明一个包含50个“double”类型元素的数组“alpha”。初始化数组,使前25个元素等于索引变量的平方,后25个元素等于索引变量的三倍

我的问题是。是索引位置的元素中被视为索引变量的值。例如,如果alpha[2]=3将是索引变量,在读取赋值时,我将取3的平方

我的另一个想法是我必须将索引编号[0]、[1]、[2]平方

谢谢你的任何意见,如果这是在错误的领域,我道歉


感谢您迄今为止的投入。我想说的是“索引变量”到底是什么


这就是我所做的

// Import various packages to be used in the program
import java.util.*;
import java.lang.*;

public class module5
{
    static Scanner console = new Scanner(System.in);

    public static void main (String[] args)
    {
        // Declare an array called alpha with 50 pre-defined elements
        double []alpha = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};

        // Process the first 25 elements
        for (int i = 0; i < 25; i++) {
            // Square the first 25 elements
            alpha[i] = Math.pow(alpha[i], 2);         
        }

        // Process the second set of 25
        for (int i = 25; i >= 25 && i < 50; i++) {
            // Multiply by 3
            alpha[i] = alpha[i] * 3;
        }


        for (int i = 0; i < alpha.length; ++i) {
            System.out.print(alpha[i]);

            if (i % 10 == 9) {
                System.out.println();
            } else {
                System.out.print(" ");
            }
        }
    }
}
//导入要在程序中使用的各种包
导入java.util.*;
导入java.lang.*;
公共类模块5
{
静态扫描仪控制台=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
//声明一个名为alpha的数组,其中包含50个预定义元素
双[]α={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,44,45,46,48,50};
//处理前25个元素
对于(int i=0;i<25;i++){
//将前25个元素平方
α[i]=数学功率(α[i],2);
}
//处理第二组25
对于(int i=25;i>=25&&i<50;i++){
//乘以3
α[i]=α[i]*3;
}
对于(int i=0;i
No.
alpha[2]=3
2
是索引变量,
3
是被索引的值。我的另一个想法是,我必须将索引编号[0]、[1]、[2]进行平方,这是正确的

我认为,您应该使用一个带有条件的循环(但也可以使用两个具有不同初始和终端条件的独立循环)来完成这些赋值

您正在处理
int
(s),因此请使用
int[]
。大概

int[] alpha = new int[50];
然后您可以使用一个
来执行
循环

for (int index = 0; index < alpha.length; index++) {
    if (index < 25) {
        alpha[index] = index * index;
    } else {
        alpha[index] = index * 3;
    }
}

数组元素的索引是括号中的数字[]。
在该示例中,alpha[2]=3,2是索引,3是存储在索引2中的变量

还要记住,对于数组,索引2意味着它是数组中的第三个位置,因为第一个索引是位置0


在本练习中,您将声明一个索引变量i,并在数组中迭代,使alpha[0]=0,alpha[1]=1,alpha[2]=4。。。alpha[i]=i*i

-谷歌关于“数组索引java”的第一个链接
,如果alpha[2]=3是索引变量
no,3是索引2处元素的值。希望你的问题更清楚一点
int i=2;α[i]=3--i是索引变量,2是索引位置,3是索引i的值。我想你的教授可能在寻找alpha[0]=0,alpha[1]=1,alpha[2]=4,alpha[3]=9…直到索引24。那么25到49是指数的三倍,所以alpha[25]=75,等等……谢谢。我确实设计了两个不同的循环,但我认为我走错了方向。请编辑您的问题以包含您的尝试,我将尝试帮助您解决此问题。感谢您的输入和建议!。。。最多24小时。25-49需要3个索引。是的,我使得我<25,然后25-49需要一个不同的for循环或者for循环中的一个条件。
for (int index = 0; index < alpha.length; index++) {
    if (index < 25) {
        alpha[index] = index * index;
    } else {
        alpha[index] = index * 3;
    }
}
for (int index = 0; index < 25; index++) {
    alpha[index] = index * index;
}
for (int index = 25; index < alpha.length; index++) {
    alpha[index] = 3 * index;
}
for (int index = 0; index < alpha.length; index++) {
    alpha[index] = index * ((index < 25) ? index : 3);
}