Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25

Thread: for loop

  1. #21
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The indentation is all over the place, and this may be not be helping.

    Java Code:
    public class Premier
    {
       public static void main (String [] args)
       {
          System.out.print("Exercice 3\npartie 1\n");       // Partie 1
        
          int nbdiviseurs = 0;
          boolean premier = true;
        
          for(int valeur = 1; valeur <= 1000; valeur++)
          {
             premier = true;
             nbdiviseurs = 0;
            
             for(int diviseur = 2; diviseur < valeur; diviseur++)
             {
                // inside this loop nbdiviseurs means "the number of divisors found
                // so far"
                if(valeur % diviseur == 0)
                {
                   nbdiviseurs++;
                   premier = false;
                    
                   if(nbdiviseurs == 1) // ie this is the *first* divisor
                   {
                      System.out.print(valeur+" est divisible par : ");
                   }
    
                   System.out.print(diviseur+","); // this is printed for *all* divisors
                }
                
             }
             // now nbdiviseurs means "the *total* number of (proper) divisors"
             if(nbdiviseurs == 0) // 0 means we have a prime
             {
                System.out.println(valeur+"  est premier");      
             }
    
             if(nbdiviseurs > 1) // >1 means we have a composite
             {
                System.out.print(" soit "+nbdiviseurs+" diviseurs\n");
             }
            
             if(nbdiviseurs == 1) // 1 means we have the square of a prime
             {
                System.out.print(" soit "+nbdiviseurs+" diviseur\n"); 
             }
          }
       }
    }

    The reason why "valeur est divisible par" prints only once per valeur is that the "if(nbdiviseurs == 1)" means that the message is only printed for the *first* diviseur. Inside the diviseur loop the if statement means "if this is the first divisor that we have seen"

    Outside (after) the loop the if statement looks similar but it has a different meaning: something like "if the total number of divisors is 1"

  2. #22
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I meant to add: well done in avoiding the List if you haven't come across these yet.

  3. #23
    newbie79 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    OMG thks a lot...it makes much more sense now...thank you for you patience

  4. #24
    newbie79 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Also you explain really well

  5. #25
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  2. Do While Loop
    By s4rd59 in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 03:29 PM
  3. Help on Do-While loop
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 10-30-2009, 04:09 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 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
  •