TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Randeep Kaur
NA
5
2.2k
Help me in this code ...:D
Jan 8 2012 10:19 PM
You can create a random number that is at least min but
less than max using the following statements:
Random ranNumberGenerator = new Random();
int randomNumber;
randomNumber = ranNumberGenerator.Next(min, max);
Write a console-based program that generates a random
number between 1 and 10. (In other words, min is 1 and max
is 11.) Ask a user to guess the random number, then display
the random number and a message indicating whether the
user's guess was too high, too low, or correct.
Here's my code, its working fine , but still i feel i can modify it in a better way, please help me out .And correct me if i am wrong somewhere in this code.
static
void
Main(
string
[] args)
{
int
min;
int
max;
int
randomNumber1;
int
randomNumber2;
string
input1;
string
input2;
string
input3;
Console
.WriteLine(
"TO genearte random number , you are supposed to enter"
+
"two numbers that is minimum number and maximum number "
);
Console
.Write(
"Enter the minimum number :"
);
input1 =
Console
.ReadLine();
min =
Convert
.ToInt32(input1);
Console
.WriteLine(
"Enter the maximum number :"
);
input2 =
Console
.ReadLine();
max =
Convert
.ToInt32(input2);
Random
ranNumberGenerator =
new
Random
();
randomNumber1 = ranNumberGenerator.Next(min, max);
Console
.WriteLine(
"Guess the random number :"
);
input3 =
Console
.ReadLine();
randomNumber2 =
Convert
.ToInt32(input3);
if
(randomNumber2 == randomNumber1)
{
Console
.WriteLine(
"Congratulations Your guess is right , the random numbr is :{0}"
, randomNumber1);
}
else
if
(randomNumber2 < randomNumber1)
{
Console
.WriteLine(
"Your guess is too low"
);
}
else
if
(randomNumber2 > randomNumber1)
{
Console
.WriteLine(
"Your guess is too high"
);
}
else
{
Console
.WriteLine(
"Invalid Output"
);
}
}
}
}
Reply
Answers (
1
)
Loop problem help plz
Multiple Catch in C#