Results 1 to 5 of 5
- 02-16-2010, 04:24 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Math.random in array and reverse order
Hello, I have an unsolved program.
I'm required to get user input on number of random numbers to appear.
e.g if user enter 3, then 3 lines of random numbers will appear.
Thereafter, the 3 random generated numbers MUST be reversed.
Here is the required output:
Enter the number of random numbers : 3
Here are the 3 random numbers:
0.123456789
0.246891357
0.369123456
Here they are again, in reverse order:
0.369123456
0.246891357
0.123456789
Requirements:
Use Scanner class
use Math.random
here is my unfinished code:
Thank you for your help everyone !Java Code:import java.util.*; class RandomNumbers{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; int [] array = new int[n]; System.out.print("Enter the number of random numbers: "); n = scan.nextInt(); System.out.println("Here are the "+n+" random numbers:"); for(int a=0; a<n.length; a++) { n[a]=(int)(Math.random()); System.out.println(n[a]); } System.out.println("Here they are again, in reverse order:"); } }
- 02-16-2010, 11:44 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Well, you did get pretty far, just a bit more and it's done. My reccomendation is this, you use int a as the couter in your for loop, why not define it outside the loop, so you can use it after the first loop is over? That way, you already a counter that points to the end of the array, all you would have to do before using it again is a--;.
- 02-16-2010, 02:45 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 19
- Rep Power
- 0
As the size of the array is dinamic - the user says how many numbers it must be stored - shouldn't you use ArrayList? Or there is a limit about how many numbers the user can asked?
Your code doesn't compile in my environment.
- 02-16-2010, 03:47 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
The size of the array is no problem:
Now, I'll give you a pretty big hint (to the OP), just fill in the blanks:Java Code:Scanner s = new Scanner(System.in); System.out.println("How many numbers do you wish to generate?"); int size = s.nextInt(); double[] result = new double[size];
Just figure out the logic of the second for loop, and your assignment is solved.Java Code://you existing code import java.util.Scanner; public class NumGen { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("How many numbers do you wish to generate?"); int size = s.nextInt(), counter; double[] result = new double[size]; for(counter = 0; counter < size; couter++) { result[counter] = Math.random(); System.out.println(result[counter]); } counter--; for( ; ; ;) //just fill in this for loop System.out.println(result[counter]); } }
- 02-17-2010, 03:33 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Thank you everyone. The assignment is complete. here is the full code.
Java Code:import java.util.*; class Program1{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; System.out.print("Enter the number of random numbers: "); n = scan.nextInt(); double [] array = new double[n]; System.out.println("Here are the "+n+" random numbers:"); for(int a=0; a<array.length; a++) { array[a]=(Math.random()); System.out.println(array[a]); } System.out.println("Here they are again, in reverse order:"); for(int a=array.length-1;a>=0; a--){ System.out.println(array[a]); } } }
Similar Threads
-
Array of instances using Math.random()
By xgi1008 in forum New To JavaReplies: 16Last Post: 01-25-2011, 11:10 PM -
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01-27-2010, 01:26 AM -
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09-14-2009, 09:28 AM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks

Bookmarks