0
Array
Im trying to write a code that accepts 8 digits into an array & then calls a method to display each digit in the array. Each time I run it, it displays the incorrect values from the array. Any help would be great. Thx! https://code.sololearn.com/cmWRPg6MrY0I/?ref=app
8 ответов
+ 2
import java.util.Scanner;
public class ArrayMethodDemo {
public static void main(String[] args) {
int[] newData = new int[8];
Scanner input = new Scanner(System.in);
for(int i = 0; i < newData.length; ++i)
{
System.out.print("Enter a Digit>>> ");
newData[i] = input.nextInt();
}
displayAll(newData);
}
public static void displayAll(int newData[])
{
for(int i = 0; i < newData.length; ++i)
System.out.print(newData[i]);
}
}
+ 1
Delete line 16
+ 1
And in line 15 write write newData[i] = input.nextInt();
+ 1
And your method display is bad
+ 1
And it is not an arraylist
0
thx!
0
thank you
- 1
use println() rather
System.out.println("Enter a Digit>>> "); // println() for print to new line
// i instead 0 because you fill all array not only first element
newData[i] = input.nextInt();
++newData[i]; // same i instead 0
// same [i] instead [0] because you want print all elements not only first 8x
for(int i = 0; i < newData.length; ++i)
System.out.print(newData[i]);
then other System.out.print(newData[..]); after for() you can delete