Write a program that reads the number of integers n, and then reads the numbers separated by a space, and writes to the array, but something does not work
import java.util.Scanner;
class Class {
public static void main (String [] args) {
Scanner n = new Scanner (System.in);
Scanner scan = new Scanner (System.in);
int k [] = new int [n];
k = scan.nextInt ();
System.out.println ( "Filling an array");
System.out.print (k + "");
}
}
Answer 1, Authority 100%
You do not have a cycle in which the numbers are written to the array, and you have not displayed.
If you have a certain number of elements, then the code would be more correct to look something like this:
import java.util.Scanner;
class Class {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
int k [] = new int [10];
for (int i = 0; i & lt; k.length; i ++) {// search of all elements
k [i] = scan.nextInt ();
}
System.out.println ( "Filled with an array");
for (int i = 0; i & lt; k.length; i ++) {// search of all elements
System.out.print (k [i] + "");
}
}
}
That is, if I have correctly understood the essence of your question.
Clarification by the condition:
import java.util.Scanner;
class Class {
public static void main (String [] args) {
System.out.println ( "Number of elements in the array:");
Scanner n = new Scanner (System.in);
int num = n.nextInt ();
System.out.println ( "Array elements");
Scanner scan = new Scanner (System.in);
int k [] = new int [num];
for (int i = 0; i & lt; k.length; i ++) {
k [i] = scan.nextInt ();
}
System.out.println ( "Filled with an array");
for (int i = 0; i & lt; k.length; i ++) {
System.out.print (k [i] + "");
}
}
}