Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Replacing the previous random number

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Replacing the previous random number

    import java.util.Scanner;
    	public class test11{
    	public static void main( String[] args ) {
    		int[] RandomValue = new int[100];//creating 100 random numbers
    			for ( int r=0; r<RandomValue.length; r++){
    				RandomValue[r] = (int)(Math.random()*9 + 1 );}//the range of random number is from1-100
    		int n=0;
    	System.out.println(RandomValue[n]
    						+" > "+RandomValue[n+1]+" > "+RandomValue[n+2]);
    					}
    					}
    Hello.
    Basically I want RandomValue[n+1] to replace n when I press enter and so on.
    for example:
    1 > 4 > 3
    (press enter)
    4 > 3 > 6
    (press enter)
    (looping...)
    (6 is the new random value(RandomValue[n+3]) and 4(RandomValue[n+1]) replaced 1(RandomValue[n]) and 3(RandomValue[n+2]) replaced 4(RandomValue[n+1])
    I've tried using for loop and while loop but the result was not I expected
    Could someone please help?Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Replacing the previous random number

    I want RandomValue[n+1] to replace n when I press enter
    Are you asking how to assign a value to the variable: n?
    n = RandomValue[n+1]; // replace n's value with contents of the array at n+1

    How does the program know when you have pressed "Enter"?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Replacing the previous random number

    'n' as a random number and 'n' as an index value are the same variable? That doesn't make sense, or would seem to be a source of unpredictable behavior.

    The array RandomValue[] should begin with a lowercase letter, randomValue[].

Similar Threads

  1. Random number
    By Imran Ahmad in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2012, 11:53 AM
  2. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 09:50 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM