Results 1 to 2 of 2
  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Reverse numbers using java arrays

    Hello I am trying to do this assignment but I can't get the needed output. Can any one help me
    what I am missing? Thank you so much!
    Assignment
    Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

    Program is written to a class called ReverseNumbers.
    Example output


    How many floating point numbers do you want to type: 5
    Type in 1. number: 5,4
    Type in 2. number: 6
    Type in 3. number: 7,2
    Type in 4. number: -5
    Type in 5. number: 2

    Given numbers in reverse order:
    2.0
    -5.0
    7.2
    6.0
    5.4

    My code:

    Java Code:
    import java.util.Scanner;
    public class apples {
    	public static void main(String[] args) {
    		Scanner reader = new Scanner(System.in);
    
    		double[] numbers;
    		
    		System.out.print("How many floating point numbers do you want to type: ");
    		int size = reader.nextInt();
    		numbers = new double[size];
    
    		for (int i=1; i <= numbers.length; ++i) {
    			System.out.print("Type in "+(i)+". number:");
    			numbers[i-1] = reader.nextInt();
    			
    		}
    
    		System.out.println();
    		System.out.println("Given numbers in reverse order:");
    
       		for (int i=numbers.length-1; i <= 0; --i) {
    			numbers[i-1] = reader.nextInt();
    			
    			System.out.println( numbers[i]);
    		}
    	}
    }

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Reverse numbers using java arrays

    Ok, well what are you getting that is incorrect? What is your question? What is the program doing that you don't expect it to?



    Without getting those answers, all I can say is this:
    Java Code:
    for (int i=numbers.length-1; i <= 0; --i) {
                numbers[i-1] = reader.nextInt();
                 
                System.out.println( numbers[i]);
            }
    That is your printout. It doesn't make sense to me. What are you doing here?

    I won't fix it, but I'll tell you this - this is much easier than you think it is. You have an array, how would you print out the numbers in an array normally?
    Java Code:
    for(int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
    }
    Simply modify that to print out in reverse. And make sure to pay VERY close attention to your checking statement.
    Last edited by AlexGraal; 02-27-2014 at 03:01 AM.

Similar Threads

  1. For loops and arrays in reverse
    By Aimforthehead in forum New To Java
    Replies: 4
    Last Post: 10-17-2011, 05:49 PM
  2. Replies: 9
    Last Post: 05-07-2011, 11:15 PM
  3. Replies: 8
    Last Post: 02-08-2011, 07:27 AM
  4. Java program problem.. Arrays.. Random Numbers
    By Chewart in forum New To Java
    Replies: 16
    Last Post: 11-16-2009, 10:21 PM
  5. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 01:09 AM

Posting Permissions

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