0

Need help to debug this code, please 👋!

Task: 1. Enter the number n from the console; 2. Enter an array of 10 numbers from the console; 3. Process the array as follows: multiply element 1 by element 10, element 2 by element 9, and so on. 4. Print the resulting array to the console. My attempt: package General; import java.util.*; import static java.lang.System.*; public class Main { public static void main(String[] args) { // write your code here out.print("Enter the size of array: "); Scanner number = new Scanner(in); int arraySize = number.nextInt(); int[] array = new int[arraySize]; int len = array.length; int i = 0; while (i < len) { array[i] = number.nextInt(); i++; } out.println(Arrays.toString(array)); for (int a = 0; a < len; a++) { for (int b = len-1; b > 0; b--) { array[i]=array[a] * array[b]; } } out.print(Arrays.toString(array)); } } I wrote it in Intellij

21st Jun 2021, 6:52 AM
Anthony
Anthony - avatar
2 Answers
+ 2
SoloLearn playground doesn't seem to play nice with static imports. You never set i back to 0 before using it in your nested loop, thereby you are going beyond the end of the array. You don't need to use, and shouldn't, a nested loop to perform the calculations on the array. The resulting array will only be half the length of the original array (assuming even as according to info n is always equal to 10). The use of len is redundant. This code should work based on the given description. https://code.sololearn.com/cP300J6c76x5/?ref=app
21st Jun 2021, 8:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
ChaoticDawg thank u so much!!! I really appreciate it )
21st Jun 2021, 9:26 AM
Anthony
Anthony - avatar