...

View Full Version : Why does this while loop iterate



sabi
03-20-2012, 06:02 AM
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);
}
}
}

sabi
03-20-2012, 06:12 AM
oh sorry wrong program

Fou-Lu
03-20-2012, 04:15 PM
It doesn't.
Although your original code posted did since it contained a semi-colon at the end of a control structure.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum