Note that there are some explanatory texts on larger screens.

plurals
  1. POprintln/print and spacing (beginning java)
    primarykey
    data
    text
    <p>My programming assignment was to print an array with 10 random integers and then have 4 lines with different outputs (every even element, reverse order, etc.)</p> <p>The code itself works fine (as far as I can tell) but one problem I'm having is that I had to put a System.out.println(""); before every line in order for the lines to look correct. </p> <p>Initially, when I had it System.out.println("[LINE 1]....") *for loop * System.out.print("arr[i] + ", ") *close for loop * it printed each integer on a separate line instead of all on one line. Am I missing something here?? Can anyone help? </p> <p>Here's my code:</p> <pre><code>import java.util.*; public class RandomInteger { public static void main(String[] args){ Random random = new Random(); int arr[]=new int[10]; System.out.print("The array of random numbers: "); for (int i = 0; i &lt; arr.length; i++) { arr[i] = random.nextInt(50); System.out.print(arr[i] + ", "); } System.out.println(""); System.out.print("[LINE 1] Elements at an even index: "); for (int i = 0; i &lt; arr.length; i++) { if(i%2==0){ System.out.print(arr[i]+" (at index "+i + "), "); } } System.out.println(""); System.out.print("[LINE 2] Every even element:"); for (int i = 0; i &lt; arr.length; i++) { if(arr[i]%2==0){ System.out.print(arr[i] + ", "); } } System.out.println(""); System.out.print("[LINE 3] Elements in reverse order: "); for(int i=arr.length-1;i&gt;=0;i--){ System.out.print(arr[i] + ", "); } System.out.println(""); System.out.print("[LINE 4] First Element is: "+arr[0]+" and Last ELement is: "+arr[arr.length-1]); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload