Results 1 to 5 of 5
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default For loops and arrays in reverse

    Java Code:
    public class Reverse {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		
    		//Declare variables
    		int[] arrays;
    		arrays = new int[10];
    		
    		//User Input
    		System.out.println("Please enter 10 integers: ");
    				arrays[0] = in.nextInt();
    		arrays[1] = in.nextInt();
    		arrays[2] = in.nextInt();
    		arrays[3] = in.nextInt();
    		arrays[4] = in.nextInt();
    		arrays[5] = in.nextInt();
    		arrays[6] = in.nextInt();
    		arrays[7] = in.nextInt();
    		arrays[8] = in.nextInt();
    		arrays[9] = in.nextInt();
    		
    		//Print in reverse
    		for();
    		System.out.println();
    This is what I have so far..with a few empty spaces. I read through his instructions and found he wanted us to read in the arrays(10) in a for loop, and then reverse them...and I'm a little lost as to how to do that. I was going to just read in 10 arrays, then print out each one starting with the last, but how would a for loop work with that?
    Last edited by Aimforthehead; 10-17-2011 at 04:47 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: For loops and arrays in reverse

    This is what who wanted? We don't know who 'he' is or what the assignment is. Perhaps showing the assignment would be helpful. For how to use loops, head here: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Also check out while, and do-while.

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: For loops and arrays in reverse

    My professor(...?) gave the instructions and the assignment is to take in 10 integers from user input put them in an array and print them in reverse, which I thought I explained...?
    I know how to use for loops, I know how to use arrays. I just don't know how to use them for this specific case...if that makes sense.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loops and arrays in reverse

    Complete the code below and print out the values of i (and j where applicable) in the body of the loops and see how those loops work:

    Java Code:
    int[] a= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // just an array
    
    for (int i= 0; i < a.length; i++) ...
    for (int i= a.length-1; i >= 0; i--) ...
    for (int i= a.length; --i >= 0; ) ...
    for (int i= a.length; i-- > 0; ) ...
    for (int i= 0; i < a.length; i++) {
       int j= a.length-i-1;
       ...
    }
    for (int i= 1; i <= a.length ; i++) {
       int j= a.length-i;
       ...
    }
    You fill in the dots by printing i (and j were applicable) and you know how loops work for the rest of your life.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: For loops and arrays in reverse

    I looked through my book and finally found what to do!
    Java Code:
    public class Reverse {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		
    		//Declare variables
    		int[] arrays;
    		arrays = new int[10];
    		int i;
    		
    		//User Input
    		System.out.println("Please enter 10 integers: ");
    		arrays[0] = in.nextInt();
    		arrays[1] = in.nextInt();
    		arrays[2] = in.nextInt();
    		arrays[3] = in.nextInt();
    		arrays[4] = in.nextInt();
    		arrays[5] = in.nextInt();
    		arrays[6] = in.nextInt();
    		arrays[7] = in.nextInt();
    		arrays[8] = in.nextInt();
    		arrays[9] = in.nextInt();
    		
    		//Print in reverse
    		for(i = arrays.length -1; i >= 0; i--)
    		
    		System.out.print(arrays[i]);
    	}
    }
    Thanks though :)

    *EDIT
    Jos I am going to be changing my code and will use yours when I understand what I'm doing...thanks.
    Last edited by Aimforthehead; 10-17-2011 at 05:56 PM.

Similar Threads

  1. Java Arrays & Loops
    By AdamG in forum New To Java
    Replies: 2
    Last Post: 10-14-2011, 05:14 PM
  2. How to reverse a string using while loops?
    By JavaS in forum New To Java
    Replies: 6
    Last Post: 03-08-2011, 03:01 AM
  3. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM
  4. A few questions about arrays and loops
    By Jamison5213 in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 05:59 AM
  5. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •