88.6k views
3 votes
Java programming

Write an application (MyNumbers) that declares an integer array of ten numbers and populates them (using a for loop) by starting with 5 and adding five more to each number. Use another for loop to print out each element of the array (with a tab in between each number).

Change the value of 35 to a 77 and reprint out the array once again (putting in a newline character first). The final result should look like this:

5 10 15 20 25 30 35 40 45 50

5 10 15 20 25 30 77 40 45 50

1 Answer

2 votes

Final answer:

The MyNumbers Java application declares an integer array, populates it using a for loop, prints the array, changes a specific value, and reprints the modified array to match the desired output.

Step-by-step explanation:

To create a Java application called MyNumbers that meets the requirements provided, we will write a program that accomplishes the following:

  • Declares an integer array to hold ten numbers.
  • Populates the array using a for loop, starting with 5 and incrementing by 5 for each subsequent element.
  • Prints out the array elements with a tab in between each number using another for loop.
  • Changes the value of the element containing 35 to 77.
  • Reprints the array, starting with a newline character.

Here is the code for the MyNumbers application:

public class MyNumbers {
public static void main(String[] args) {
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = 5 + (i * 5);
}
for (int number : numbers) {
System.out.print(number + "\t");
}
System.out.println();
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 35) {
numbers[i] = 77;
break;
}
}
for (int number : numbers) {
System.out.print(number + "\t");
}
}
}

This will produce the desired output:

5 10 15 20 25 30 35 40 45 50
5 10 15 20 25 30 77 40 45 50
User Jswanson
by
8.0k points