Results 1 to 11 of 11
  1. #1
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default Loop problem (while)

    Hello,

    I post again on this Fourm because I have a problem with the while and or place
    his braces. The following program works if I enter a single number, but I want to
    execute this piece of code until the user enters -1 (I do it so it does not appear in the following code):

    Java Code:
        if (nombreEntre > 0 && nombreEntre <= 1000000)
     
            {
                compteurNombres++;
                additionNombres = nombreEntre;
            }
     
            if (nombreEntre < 2) // Si le nombre entré est plus petit que 2
            {
                premier = false;
            }
     
            while (compteurNombrePremier * compteurNombrePremier <= nombreEntre)
            {
     
                if (nombreEntre % compteurNombrePremier == 0)
                /* Si le résultat de l'un des modulos des nombres testés = 0,
                alors il est composé */
                {
                    premier = false;
                }
                compteurNombrePremier = compteurNombrePremier + 1;
            }
     
            if (premier) // Si le nombre est premier
            {
                System.out.println(nombreEntre + " est un nombre premier");
            }
            else // Si le nombre n'est pas premier
            {
                System.out.println(nombreEntre + " n'est pas un nombre premier");
            }
     
            while (nombreEntre >= racineCarree * 2 + 1)
            {
                nombreEntre -= (racineCarree * 2 + 1);
                racineCarree++;
            }
     
     
            if (racineCarree * racineCarree != nombreEntre)
            /* Si le carré de la racine n'égale pas le nombre entré,
            alors elle est approximative */
            {
                System.out.println("La racine entière approximative est"
                        + " " + racineCarree);
                racineApprox++;
                // Le nombre de racines approximatives trouvées augmente de 1
                compteurRacines++;
            }
            else
            {
                System.out.println("La racine entière exacte est " + racineCarree);
                /* Si le carré de la racine égale au nombre entré, alors elle est
                exacte */
                racineExacte++; /* Le nombre de racines exactes trouvées
                augmente de 1 */
                compteurRacines++; // Le nombre de racines trouvées augmente de 1
            }
    I really do not know where to place it. I have tried many times, but still the same result: It gets stuck in an infinite loop.

    Thank you for your help

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by roud9 View Post
    I have tried many times, but still the same result: It gets stuck in an infinite loop.

    bonjour. in order to stop the loop you need somewhere to get the input of the user and then decide whether to continue or break.

  3. #3
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default

    I had added this at the begenning :

    Java Code:
            if (nombreEntre > 0 && nombreEntre <= 1000000)
            {
     
                compteurNombres++;
                additionNombres = nombreEntre;
                while (nombreEntre > 0 && nombreEntre <= 1000000)
                {
                    System.out.println("Entrez le nombre dont vous voulez" + " extraire la racine carrée ");
                    nombreEntre = Integer.parseInt(r.readLine());
                }
            }
    but it gets stuck in the loop and always ask me a number

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    What happens if you enter zero or a negative number or a number bigger than one million?

  5. #5
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default

    It stops the program but I want if number < -1 and > 1000000 it will ask me enter a other number

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Do you see how the conditions being tested in your loop control when you exit the loop?
    When do you want to exit the loop and
    when do you want to stay in the loop?

  7. #7
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default

    now it works when I enter numbers between 0 and one million. But the first time, it gave me a good result, the second time or after, it gave me a wrong result

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Can you explain what a wrong result is?

  9. #9
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default

    For exemple I enter 3 : prime number and sqrt without decimal gave me 1 // Correct
    The second time it askimg me a number, I enter 9: not prime and sqrt = 3 // Correct
    But the third time I re-enter 3 : not prime and sqrt = 3...
    I don't understand why it give me this and it's for all numbers

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Why does it work differently the second time you enter a 3 from the first time you enter a 3?
    Should the program give the same answer every time you enter a 3?

    This looks like a logic problem with your program.
    You need to play computer. Take a piece of paper and step through the logic step by step writing down the values of the variables as they change until you see where your logic is wrong.
    Or you could add print out statements to your code to show how variables change as the program executes and to show how the execution flow goes.

  11. #11
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  2. Problem with loop
    By Tykk in forum New To Java
    Replies: 7
    Last Post: 10-04-2009, 10:26 PM
  3. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  4. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  5. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 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
  •