Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Why does this while loop iterate

    If the char variable again is initialized to 'N' then why does this loop iterate the first time

    import java.util.Scanner;


    public class teest
    {
    public static void main (String[] args)
    {
    Scanner keyboard = new Scanner(System.in);

    int num;
    String input;
    char again = 'N';

    while(again == 'Y' || again == 'y')
    {
    System.out.println("enter num: ");
    num = keyboard.nextInt();
    System.out.println("The number entered was " + num);
    System.out.println("Do it agian?");
    keyboard.nextLine();
    input = keyboard.nextLine();
    again = input.charAt(0);
    }
    }
    }
    Last edited by sabi; 03-20-2012 at 07:06 AM.

  • #2
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    oh sorry wrong program

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    It doesn't.
    Although your original code posted did since it contained a semi-colon at the end of a control structure.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •