2

I want to put what the use puts in for x to be stored in the yourNumbers array, how would I do that? Edit: How would I clean up the println parts at the bottom, using loops.

import java.util.Scanner; 
 public class array {
public class SS_Un8As1 {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int[] yourNumbers = new int[10];
    for (int counter=0; counter < yourNumbers.length; counter++){
        System.out.print("Enter your number: ");
        yourNumbers[counter] = scan.nextInt();
     }         
   System.out.println("Original numbers: " + yourNumbers[0] + "," + yourNumbers[1] + "," + yourNumbers[2] + "," + yourNumbers[3] + "," + yourNumbers[4] + "," + yourNumbers[5] + "," + yourNumbers[6] + "," + yourNumbers[7] + "," + yourNumbers[8] + "," + yourNumbers[9]);
   System.out.println("Original numbers multiplied by five: " + yourNumbers[0]*5 + "," + yourNumbers[1]*5 + "," + yourNumbers[2]*5 + "," + yourNumbers[3]*5 + "," + yourNumbers[4]*5 + "," + yourNumbers[5]*5 + "," + yourNumbers[6]*5 + "," + yourNumbers[7]*5 + "," + yourNumbers[8]*5 + "," + yourNumbers[9]*5);
   System.out.println("Original numbers minus the next number: " + (yourNumbers[0]-yourNumbers[1]) + "," + (yourNumbers[1]-yourNumbers[2]) + "," + (yourNumbers[2]-yourNumbers[3]) + "," + (yourNumbers[3]-yourNumbers[4]) + "," + (yourNumbers[4]-yourNumbers[5]) + "," + (yourNumbers[5]-yourNumbers[6]) + "," + (yourNumbers[6]-yourNumbers[7]) + "," + (yourNumbers[7]-yourNumbers[8]) + "," + (yourNumbers[8]-yourNumbers[9]) + "," + (yourNumbers[9]-yourNumbers[0]));
}

}

5
  • 4
    It should be < 9 and not < 10 Commented Jul 24, 2013 at 20:32
  • @morpheus05 even better, he should store the length in a variable and use that Commented Jul 24, 2013 at 20:35
  • Find a good book on Java and read it. Commented Jul 24, 2013 at 20:45
  • the array has a .length field which would perform this task. Commented Jul 24, 2013 at 20:48
  • How would I clean up the bottom part of my code, my teachers says to use loops but I have no idea what she means by that. Commented Jul 24, 2013 at 21:26

3 Answers 3

3

Like this:

yourNumbers[counter] = x;

The above code is stating: store the value of x in the array yourNumbers in the index (position) counter. Because counter is the iteration variable of a for loop, each time the loop advances the counter the next value of x will be stored in the next available position in the array.

You must make sure that the length of the array is the same as the value of counter. In your code, do this:

int[] yourNumbers = new int[10];

Why? because counter goes from 0 to 9, so the array must be of length 10.

Sign up to request clarification or add additional context in comments.

3 Comments

What does the [i] stand for?
he ment counter. Its the index variable of the array.
Even better, for (int counter=0; counter < yourNumbers.length; counter++), to allow changing the length of the array in just one place.
2

replace this

int x = scan.nextInt();

with

yourNumbers[counter] = scan.nextInt();

also, the condition should be counter<9, or better counter<yourNumbers.length

Comments

1

Simply pass the input into the next element of the array, while inside the for loop:

yourNumbers[counter] = x;

I should also point out, however, that you should validate that your input is actually an integer before assigning x the value. If the value isn't an integer, your program will crash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.