main
{
quitflag = 0;
compnumber = random(1,10);
while(quitflag == 0)
{
reqbegin("Pick
a Number");
ctltext("","Pick a number between 1 and 10");
yourguess = 0;
a1 = ctlinteger("Your Guess",yourguess);
if(reqpost())
{
yourguess = getvalue(a1);
}
else
return;
reqend();
if(yourguess == compnumber)
{
info("Congratulations! You Won!");
quitflag = 1;
}
else
{
if(yourguess > compnumber)
{
info("Your guess is too big! Try again.");
}
else
{
info("Your guess is too small! Try again.");
}
}
}
}
The new code is pretty simple.
Remember, this is the else
part of the if/else
that checked to see if your number matches the computer's number. Since
this is in the else
part, we only get to this code if your number is wrong. If your guess
is wrong, that means there are only two possibilities remaining, either
your number is to big, or it is too small.
We'll check for "too big"
first.
If yourguess
is greater than the number the computer picked, compnumber,
you get a message that says your guess is too big.
Since there are only two
possibilities, if
yourguess isn't
greater than the computer's number, the only choice left is "too small,"
so the else
in this if/else
gives you a message that says your guess is too small.
Would you believe there
are only a couple of thing left to do? It's true!
* When we write quitflag
= 0, or test
+ 5, we are writing expressions. In these two expressions, =
and + are the
operators, because they act, or operate, on the other bits of the expression.
The other bits (quitflag, 0,
test, 5) are called the operands. The good news is, you can probably
live a happy and fullfilled life without ever remembering this fact.