Results 1 to 4 of 4
  1. #1
    Stanimir is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Little question about do while loop need help :)

    Hi guys i am 23 yo student and i have just started learning JAVA.i have this problem and can't make any progress although i tried for 2 hours lol :D
    i have this method called "deljiv" and i want to call this method in my public static main void.. method and when i call it it works perfectly but the problem is when i want to ask a question "do you want to play again" and if the answer is "yes" to do method "deljiv" again.I tried with while , do while,continue and somehow can't figure it out.here is my code so if you have time to fix it a little bit please do it cuzz i am breaking my head on this.

    Best regards D



    Java Code:
    package vezba4.pkg2deljivsa2;
    
    import java.util.Scanner;
    
    public class Vezba42deljivSa2 {
    
        public static void main(String[] args) {
            int number;
            String choice;//
            Scanner keyboard = new Scanner(System.in);
            System.out.println("This program calculates whether the number is divisible by 2");
            System.out.println("Insert some number");
            number = keyboard.nextInt();
            deljiv(broj);
            System.out.println("Do you want to play again ?");
          choice = keyboard.nextLine();
    
            do {
    
                System.out.println("Insert some number");
              number= keyboard.nextInt();
                deljiv(number);
    
            } while (choice.equalsIgnoreCase("yes"));
    
        }
    
        public static boolean deljiv(int n) {
            Scanner keyboard = new Scanner(System.in);
    
            if (n % 2 == 0) {
                System.out.println("number is divisible by 2");
            } else {
                System.out.println("number is not divisible by 2");
            }
            return true;
    
        }
    
    }
    Last edited by Stanimir; 03-18-2014 at 03:50 PM.

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

    Default Re: Little question about do while loop need help :)

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    To keep a loop from going forever, the code INSIDE of the loop must change the value of the variable that is controlling the looping.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Little question about do while loop need help :)

    Sounds like you want to keep playing the game until the user says that they don't want to play anymore.
    With the code that you have now, after the 1st play, the user is asked if they want to play again.
    If they enter yes, your while loop condition evaluates to true, which will repeatedly:

    -ask the user to enter a number
    -tell them whether or not the number they entered is divisible by 2 or not
    -repeat

    As Norm pointed out, you have to have your while condition evaluate to false in order to have the loop exit, i.e., choice becomes something other than 'yes'.
    Your choice variable is only being set 1 time.

    Think about that and give it a try.

  4. #4
    Stanimir is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Little question about do while loop need help :)

    Ok guys i have done it thank you for your assistance it works perfectly here is my code :D

    Java Code:
    package vezba4.pkg2deljivsa2;
    
    import java.util.Scanner;
    
    public class Vezba42deljivSa2 {
        
        public static void main(String[] args) {
            int number = 0;
            String choice;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("This program counts if the number is divisible by  2");
            
            do {
                deljiv(number);
                System.out.println("Do you want to play again ?");
                choice = keyboard.nextLine();
                
            } while (choice.equalsIgnoreCase("yes"));
            
        }
        
        public static boolean deljiv(int number) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Insert a number");
            number = keyboard.nextInt();
            
            if (number % 2 == 0) {
                System.out.println("number is divisible by  2");
            } else {
                System.out.println("number is not divisible by 2");
            }
            return true;
            
        }
        
    }

Similar Threads

  1. loop question
    By smcmahon in forum New To Java
    Replies: 3
    Last Post: 05-29-2013, 11:05 PM
  2. Question about while loop
    By sleshas in forum New To Java
    Replies: 3
    Last Post: 03-20-2012, 06:22 PM
  3. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 02:26 AM
  4. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 05:08 PM
  5. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-15-2009, 11:23 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
  •