Results 1 to 4 of 4

Thread: Loops In Java?

  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Question Loops In Java?

    Hey

    OK. I want to make a loop with 2 outcomes odd # and even #. The number is randomized from 0-10. So if even number the program continues if odd number the program repeats these steps. How would you do this? this is what i have so far.


    package Samurai;
    import java.util.*;
    public class Samurai {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //VARIABLES ONLY SECTION



    int nameofcity,cityexplore,i,k;


    cityexplore=(int) (Math.random()*10);
    cityexplore%=i;
    while (i==0)
    {
    System.out.println("Please Keep Exploring");
    i++;
    }

    }

    }



    As you can see i dont quit know how or which loop to even began with please any help would be appreciated.
    Last edited by dharav; 01-13-2012 at 05:50 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loops In Java?

    Quote Originally Posted by dharav View Post
    ...but you cannot put a loop inside of a loop. ...
    Says who? :)

  3. #3
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Default Re: Loops In Java?

    Oh i made a mistake Please Carry On

  4. #4
    Takashy is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Re: Loops In Java?

    Not sure if I got it correctly, but something like this would probably work..
    Java Code:
    int rnr = 1;
    while (rnr%2!=0)
    {
     //do whatever you need to do
    rnr = //generate a new nr.. 
    }
    It checks if dividing the rnr with 2 would leave anything behind or not. 0 (in this case) would mean that its even, 1 that its not. Sorry for not giving you more info, but I'm kinda in a hurry. Try looking it up on the internet if you have problems understanding how it works.. (which is actually pretty simple)

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. summation in java using for loops
    By java157 in forum New To Java
    Replies: 3
    Last Post: 03-17-2011, 08:33 AM
  3. Java loops
    By murali23krishna in forum New To Java
    Replies: 1
    Last Post: 02-22-2011, 04:01 AM
  4. [HELP]Java loops
    By jude113 in forum JCreator
    Replies: 0
    Last Post: 03-06-2009, 03:11 AM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 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
  •