146k views
13 votes
Create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Below is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program.Please enter the number of digits to be stored: 5Enter integer 0: -1Enter integer 1: 10Enter integer 2: 15Enter integer 3: -6Enter integer 4: 3The contents of your array:Number of digits in array: 5Digits in array: -1 10 15 -6 3

1 Answer

1 vote

Answer:

In Java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int arrsize;

System.out.print("Please enter the number of digits to be stored: ");

arrsize = input.nextInt();

int[] myarr = new int[arrsize];

for (int kount = 0; kount < arrsize; kount++) {

System.out.print("Enter integer: ");

myarr[kount] = input.nextInt(); }

System.out.println("The contents of your array:\\Number of digits in array: "+arrsize);

System.out.print("Digits in array: ");

for (int kount = 0; kount < arrsize; kount++) {

System.out.print(myarr[kount]+" "); }

}

}

Step-by-step explanation:

This declares the length of the array

int arrsize;

This prompts the user for the array length

System.out.print("Please enter the number of digits to be stored: ");

This gets the input for array length

arrsize = input.nextInt();

This declares the array

int[] myarr = new int[arrsize];

The following iteration gets input for the array elements

for (int kount = 0; kount < arrsize; kount++) {

System.out.print("Enter integer: ");

myarr[kount] = input.nextInt(); }

This prints the header and the number of digits

System.out.println("The contents of your array:\\Number of digits in array: "+arrsize);

System.out.print("Digits in array: ");

The following iteration prints the array elements

for (int kount = 0; kount < arrsize; kount++) {

System.out.print(myarr[kount]+" "); }

User Ben Xu
by
3.1k points