· Enter a do-while loop and generate a random, byte-sized, value in the range of 0 to 255. Store the random value in a variable of type byte. Display the value in hex as shown below.
· The user will input a string containing 1's and 0's. Use a do-while loop to ensure that the length of the string is 8 characters; if it is not, honk the speaker and display an error message in yellow, and obtain new input. An invalid guess is not counted as one of the five hex values. The hex value for which an incorrect number of bit was entered will be repeated again, as shown below.
· Convert the string to a byte-sized number. Compare the number with the random value. If the two values are the same, display a message indicating a correct result and increment the number of correct responses. If the values are different, display a message indicating that the user's response was incorrect.
· Continue looping until the user has been presented with five hex values.
· Display the number of correct responses.
The random byte-sized value can be generated using the random number generator. In the code shown below, the random number generator is named rndGen, and the hex value is named byHex.
byHex = (byte)rndGen.Next(256);
The user will input their binary value as a string of 1's and 0's. The string (sInput) can be converted to a byte by the following code:
byInput = Convert.ToByte(sInput, 2);
so far i have this.
[CODE]
string sInput;
Random rndGen = new Random();
byte byhex = (byte)rndGen.Next(256);
Console.Title = "ICA15 - Hex Quiz";
Console.WriteLine("{0, 40}", "ICA15 - Hex Quiz");
Console.WriteLine("\nInput the binary equivalent to the following ten hex values.");
Console.Write("\nThe binary equivalent to 0x{0} is ", Convert.ToString(byhex, 16).ToUpper());
Console.WriteLine("\nPress the
Console.ReadLine();
[/CODE]
i don't know how to use the "byInput = Convert.ToByte(sInput, 2);". and how to do it in a do-while loop.
Need Help, I'm really confuse...
Suthish NairPosted Mar 2, 2011, 3:02 AM
VulpesPosted Mar 1, 2011, 3:06 PM
However, I think they're expecting something like this:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Console.Clear();
Console.Title = "ICA15 - Hex Quiz";
Console.WriteLine("{0, 40}", "ICA15 - Hex Quiz\n");
int entry = 0;
int correct = 0;
do
{
string sInput = "";
Random rndGen = new Random();
byte byhex = (byte)rndGen.Next(256);
Console.WriteLine("\nThe random hex value is 0x{0:X2}", byhex);
ClearBuffer();
Console.WriteLine("\nEntry {0}: Please enter a sequence of eight 1's or 0's", entry + 1);
int bit = 0;
bool isValid = true;
do
{
ConsoleKeyInfo ki = Console.ReadKey();
char c = ki.KeyChar;
if (c != '1' && c != '0')
{
Console.WriteLine();
ConsoleColor oldBackgroundColor = Console.BackgroundColor;
ConsoleColor oldForegroundColor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Black;
Console.Beep();
if (c != '\r')
{
Console.WriteLine("Must be either '1' or '0', try again");
}
else
{
Console.WriteLine("Must be exactly 8 digits, try again");
}
Console.BackgroundColor = oldBackgroundColor;
Console.ForegroundColor = oldForegroundColor;
isValid = false;
break;
}
sInput += c.ToString();
bit++;
}
while (bit < 8);
Console.WriteLine();
if (!isValid) continue;
byte byInput = Convert.ToByte(sInput, 2);
if (byInput == byhex)
{
Console.WriteLine("Your response was correct!");
correct++;
}
else
{
Console.WriteLine("Sorry, your response was wrong :(");
}
entry++;
}
while (entry < 5);
Console.WriteLine("\nThe total number of correct responses was {0}", correct);
Console.WriteLine("\nPress the
Console.ReadLine();
}
static void ClearBuffer()
{
while (Console.KeyAvailable) Console.ReadKey(true);
}
}
}