PC Review


Reply
Thread Tools Rate Thread

Can not break out of While loop.

 
 
Mike Langworthy
Guest
Posts: n/a
 
      27th Apr 2007
I am having a problem Breaking out of the while loop, it never terminates.
any suggestions

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(1, 100);
Console.WriteLine(returnValue);

int Guess = 0;
int i = 0;

Console.WriteLine("Welcome to the Guessing game. Please Choose
a Number between 1 and 100.");

while(Guess != returnValue)
{
Guess = Convert.ToInt32(Console.Read());

if (Guess <= returnValue - 75)
{
Console.WriteLine("You are so low you are in
Hell.");
}
if (Guess <= returnValue - 50 )
{
Console.WriteLine("Put the Shovel down, you are too
Low.");
}
if (Guess < returnValue )
{
Console.WriteLine("Woah buck, too low Try again");
}
if (Guess >= returnValue + 75 )
{
Console.WriteLine("Hey Flyboy, get your head out of
the clouds");
}
if (Guess >= returnValue + 50 )
{
Console.WriteLine("Get off the damn roof you are too
high");
}
}
Console.WriteLine("Congratulations, you win ");
Console.WriteLine("Press Enter to terminate...");
Console.Read();

}




}
}

 
Reply With Quote
 
 
 
 
Ebbe Kristensen
Guest
Posts: n/a
 
      27th Apr 2007
Mike Langworthy wrote:
> I am having a problem Breaking out of the while loop, it never
> terminates. any suggestions


> Guess = Convert.ToInt32(Console.Read());


Should be:
Guess = Convert.ToInt32(Console.ReadLine());

Ebbe


 
Reply With Quote
 
Vince A
Guest
Posts: n/a
 
      14th May 2007
You probably have already figured this out by this time...

Console.Read() is reading what's typed in as a series of strings, and not as
a single integer.
Each individual character is being converted to integer.


"Mike Langworthy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I am having a problem Breaking out of the while loop, it never terminates.
>any suggestions
>
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace ConsoleApplication1
> {
> class Program
> {
> static void Main(string[] args)
> {
> Random random = new Random();
> int returnValue = random.Next(1, 100);
> Console.WriteLine(returnValue);
>
> int Guess = 0;
> int i = 0;
>
> Console.WriteLine("Welcome to the Guessing game. Please Choose
> a Number between 1 and 100.");
>
> while(Guess != returnValue)
> {
> Guess = Convert.ToInt32(Console.Read());
>
> if (Guess <= returnValue - 75)
> {
> Console.WriteLine("You are so low you are in
> Hell.");
> }
> if (Guess <= returnValue - 50 )
> {
> Console.WriteLine("Put the Shovel down, you are too
> Low.");
> }
> if (Guess < returnValue )
> {
> Console.WriteLine("Woah buck, too low Try again");
> }
> if (Guess >= returnValue + 75 )
> {
> Console.WriteLine("Hey Flyboy, get your head out of
> the clouds");
> }
> if (Guess >= returnValue + 50 )
> {
> Console.WriteLine("Get off the damn roof you are
> too high");
> }
> }
> Console.WriteLine("Congratulations, you win ");
> Console.WriteLine("Press Enter to terminate...");
> Console.Read();
>
> }
>
>
>
>
> }
> }
>



 
Reply With Quote
 
 
 
Reply

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break the FOR loop? Eric Microsoft Excel Programming 3 9th Apr 2010 05:49 AM
Break out of a loop in Excel2007? Don Microsoft Excel Misc 8 4th May 2009 09:48 PM
Break Code within While Loop =?Utf-8?B?UkZyZWNoZXR0ZQ==?= Microsoft Access VBA Modules 2 26th Oct 2007 06:37 PM
Break fr Loop =?Utf-8?B?U3dpc2g=?= Microsoft Access Macros 1 9th Nov 2005 07:43 AM
Loop for Page Break Bruce Roberson Microsoft Excel Programming 3 16th Mar 2004 08:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:42 PM.