Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-20-2012, 06:02 AM   PM User | #1
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
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 06:06 AM..
sabi is offline   Reply With Quote
Old 03-20-2012, 06:12 AM   PM User | #2
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
oh sorry wrong program
sabi is offline   Reply With Quote
Old 03-20-2012, 04:15 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It doesn't.
Although your original code posted did since it contained a semi-colon at the end of a control structure.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 AM.


Advertisement
Log in to turn off these ads.