Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java help!

  • 03-05-2001 10:29am
    #1
    Registered Users, Registered Users 2 Posts: 7,626 ✭✭✭


    ok. here's my problem. i have a random number generator.... i want to have the user enter the number of random numbers to generate (REPEAT). i want them to specify the range ofthe random numbers... called SMALL and LARGE. ok. seting up the user entering the numbers is easy. it's just getting the SMALL and LARGE to werk.

    import java.io.*;
    import java.util.*;
    public class ciaran
    {
       public static void main(String args[]) throws IOException
    
       {	
          int value = 	0;
          int REPEAT =	5;
          int SMALL =	15;
          int LARGE =	70;
    
         
    	//begin loop
       for(int i = 0; i < 10; i++)
    
    {
             value = 1 + (int) (generator.nextInt() % 100);
             if (value < 0)
                value = value * (-1);
    
    
      if (value < SMALL)
    // [b]HOW THE **** DO I MAKE THIS GO BACK TO THE START OF THE LOOP??[/b]
    
    
             System.out.println("Loop " + i + "\t" + value);
       
    
       }
    
       }
    }
    
    
    thanks if u can help!


Comments

  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    not sure if this is what you want but take this program from the point after numbers have been entered, also I'm not sure if the methods are correct but you should be able to convert easily enough.
    for(i=1;i<REPEAT;i++)
    {
      value = (int)math.floor(SMALL+ math.random()*LARGE)
      System.out.println("Loop " + i + "\t" + value + "\n");
    }
    

    not sure if this helps but then again I'm not sure what you are trying to do with your code, its a bit strange looking for what you want to do.

    However in answer to your question "// HOW THE **** DO I MAKE THIS GO BACK TO THE START OF THE LOOP??" I believe the line:
    continue;
    will go to the next iteration of the loop.

    Baz_


  • Registered Users, Registered Users 2 Posts: 7,626 ✭✭✭smoke.me.a.kipper


    ya, the continue; thing is werkin'.

    BUT,

    the program runs.... gets a random number, checks if it's smaller than 'SMALL' if it's not, then i continues, but if it is, it omits that random number and continues, sometimes giving only 1 or 2 results instead of 5.

    anyway i can say, if VALUE is less then SMALL, go back and try another number instead, not just forget and move on.


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Well yes anyone worth their salt will tell you that but if you use the code I gave you only numbers within the range you want will be produced. That is to say REPEAT numbers all in the range SMALL to LARGE. Another thing you can do is use a while loop until you get 5 valid numbers.


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    here is some more tested code, still doesn't work properly though it never gives you the large value, I'm sure there is a reason for this and I'm sure somebody else here can explain it so you shouldn't be too lost for long, anyways you'll have to change the output method, because I did this through an applet.
    int repeat, i, small, large, value;
        repeat = 5;
        small = 10;
        large = 65;
    
        for (i=1;i<=repeat;i++)
        {
          value = small + (int)(Math.floor(Math.random() * (large-small)));
          JOptionPane.showMessageDialog(null,"The number " + i + " value is " + value);
        }
    


  • Registered Users, Registered Users 2 Posts: 7,626 ✭✭✭smoke.me.a.kipper


    thanks for your help smile.gif

    i'm a bit closer now.

    it's still ****ing me off though - mad.gif

    - Ciaran
    karate-kid.gif

    [This message has been edited by smoke-me-a-kipper (edited 03-05-2001).]


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    [edit]never mind....[/edit]

    [This message has been edited by Jazz (edited 03-05-2001).]


Advertisement