如何将100添加到数组元素列表中 [英] How do I add 100 to a list of array elements

查看:76
本文介绍了如何将100添加到数组元素列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第4行:我无法弄清楚如何使anArray数组在for循环中的索引0之后为每个元素添加100?说明在评论中。有什么建议吗?



 public static void main(String [] args){
// LINE 1. DECELARE INTRAY INTEGERS
int [] anArray;

// LINE 2.为阵列中的10个INTEGERS分配内存。
anArray = new int [10];

//创建输入设备的可用实例
Scanner myInputScannerInstance = new Scanner(System.in);

//我们会要求用户输入一个整数。请注意,在本练习中,
//代码我们无法验证用户是否实际
//输入INTEGER或不是。在生产程序中,我们需要
//来验证这一点;例如,通过包含
//异常处理代码。 (按原样,用户可以键入XYZ
//,这将导致异常。)
System.out.print(输入一个整数并按回车:);

//将用户输入(以字符串形式输入,甚至
//虽然我们要求用户输入一个整数)转换为整数
int myFirstArrayElement = Integer.parseInt (myInputScannerInstance.next());

// LINE 3.使用CONVERTED INTEGER初始化第一个阵列元素myFirstArrayElement
anArray [0] = myFirstArrayElement;

// LINE 4.通过将每个元素添加100来初始化第二个元素。
//示例:第二元素的价值是第一元素的价值100;
//第三元素的价值是第二元素的价值100;等等。
for(int j = 0; j< anArray.length; j ++){
if(anArray [j]!= anArray [0]){
anArray [j] + = 100 ;
}

}


// LINE 5.根据顶级模型显示阵列中每个元素的价值-OF-CODE评论。
Arrays.sort(anArray); //发送我们的Anarray数组,按sort()方法按升序排序
int i; //定义循环的计数器。我们可以这样做或者在
// for for循环中,就像我们在前面的for循环中所做的那样。

for(i = 0; i< anArray.length; i ++){//循环,从0开始,每次加1,直到计数器小于数组长度。
System.out.println(索引处的元素+ i +:+ anArray [i]); //打印数字
}

}

}





我的尝试:



我已为代码完成了其他所有工作。我无法弄清楚第4行。请帮忙。谢谢。

解决方案

下面的内容应该这样做。从1开始,因为第3行用输入值初始化了索引0。然后根据你的要求,array [1] = index [0] + input或anArray [0]等等......



  for  int  j = 1; j< anArray.length; j ++){
anArray [j] = anArray [j- 1 ] + anArray [ 0 ];
}





输出:

输入一个整数并点击返回:100

索引0的元素:100

索引1:200的元素

索引2的元素:300

索引为3的元素:400

元素在指数4:500

元素在指数5:600

元素在指数6:700

元素在索引7:800

元素在索引8:900

元素在索引9:1000


  for  int  j = 1; j< anArray.length; j ++)
{
anArray [j] = anArray [j- 1 ] + 100 ; // j是当前项目,(j-1)是前一项
}


On line 4: I can not figure out how to make the anArray array add 100 to each element following index 0 in the for loop? The instructions are in the comment. Any suggestions?

public static void main(String[] args) {
        // LINE 1. DECLARE AN ARRAY OF INTEGERS
        int[] anArray; 
        
        // LINE 2. ALLOCATE MEMORY FOR 10 INTEGERS WITHIN THE ARRAY.
        anArray = new int[10];
        
        // Create a usable instance of an input device
        Scanner myInputScannerInstance = new Scanner(System.in); 
        
        // We will ask a user to type in an integer. Note that in this practice
        // code  WE ARE NOT VERIFYING WHETHER THE USER ACTUALLY
        // TYPES AN INTEGER OR NOT. In a production program, we would
        // need to verify this; for example, by including
        // exception handling code. (As-is, a user can type in XYZ
        // and that will cause an exception.)
        System.out.print("Enter an integer and hit Return: ");
        
        // Convert the user input (which comes in as a string even
        // though we ask the user for an integer) to an integer
        int myFirstArrayElement = Integer.parseInt(myInputScannerInstance.next());
        
        // LINE 3. INITIALIZE THE FIRST ARRAY ELEMENT WITH THE CONVERTED INTEGER myFirstArrayElement
        anArray[0] = myFirstArrayElement;
        
        // LINE 4. INITIALIZE THE SECOND THROUGH THE TENTH ELEMENTS BY ADDING 100 TO THE EACH PRECEDING VALUE.
        // EXAMPLE: THE VALUE OF THE SECOND ELEMENT IS THE VALUE OF THE FIRST PLUS 100;
        // THE VALUE OF THE THIRD ELEMENT IS THE VALUE OF THE SECOND PLUS 100; AND SO ON.
        for (int j=0; j < anArray.length; j++) {
            if (anArray[j]!= anArray[0]){
                anArray[j] += 100;
            }
           
        } 

        
        // LINE 5. DISPLAY THE VALUES OF EACH ELEMENT OF THE ARRAY IN ASCENDING ORDER BASED ON THE MODEL IN THE TOP-OF-CODE COMMENTS.
        Arrays.sort(anArray);		// Send our Anarray array to be sorted in ascending order by the sort() method
        int i;				// Define the counter for the loop. We can do it this way or inside
                                        // the for loop, as we did in the preceding for loop.
                                        
        for (i=0; i < anArray.length; i++) {	// Loop, starting at 0 and adding 1 each time, until counter is less than array length. 		
              System.out.println("Element at index " + i + ": " + anArray[i]);// print the numbers
        }

    }
    
}



What I have tried:

I have done everything else for the code. I cant figure out line 4. please help. Thanks.

解决方案

Something like below should do it. Start with 1 because the line 3 initialized the index 0 with the input value. Then based on your requirement, array[1] = index[0] + input or anArray[0] , and so on...

for (int j=1; j < anArray.length; j++) {
    anArray[j] = anArray[j-1] +anArray[0];
}



Output:
Enter an integer and hit Return: 100
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000


for (int j=1; j < anArray.length; j++) 
{
  anArray[j] = anArray[j-1] + 100; // j is the current item, (j-1)  is the previous one
}


这篇关于如何将100添加到数组元素列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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