Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2003
    Location
    Here
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My random number script....

    I decided to add a if fuction, where if the random number was 1 more or less than the guess something else will be printed. There is a problem, and when it does get to 1 more/ less, it just says that it is wrong. My script is:

    <?php
    $Guess = 3;
    $Between1 = 1;
    $Between2 = 10;
    $Number = rand($Between1,$Between2);
    print ("
    <center>

    The number is...
    <font color=green><b>$Number</b></font>
    <p><p>Your guess was...
    <font color=green><b>$Guess</b></font><p><p>

    </center>
    ");
    if ($Guess == $Number) {
    $Correct = Yes;
    print ("<center>You got it correct!</center>");
    } elseif ( ("$Guess - 1" > $Number) or ("$Guess + 1" < $Number) ) {
    $Correct = No;
    print ("<center>You got it wrong!</center>");
    } elseif ($Number == "$Guess +- 1")
    $Correct = Nearly;
    print ("<center>You got it nearly correct!</center>");
    }
    print ("<p>");
    if ($Correct == Yes) {
    print ("<center>You win 700 Credits!</center>");
    } elseif ($Correct == No) {
    print ("<center>You win 0 Credits!</center>");
    } elseif ($Correct == Nearly) {
    print ("<center>You win 500 Credits!</center>");
    }

    ?>

    Please post if you can fix it

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:

    if($Guess == $Number){
        
    $Correct "Yes";
        print (
    "<center>You got it correct!</center>"); 
    }
    elseif((
    $Guess == $Number-1) || ($Guess == $Number+1)){
        
    $Correct "Nearly"
        print (
    "<center>You got it nearly correct!</center>"); 
    }
    else{
        
    $Correct "No"
        print (
    "<center>You got it wrong!</center>"); 


    print (
    "<p>"); 

    if (
    $Correct == "Yes") { 
        print (
    "<center>You win 700 Credits!</center>"); 
    }
    elseif(
    $Correct == "Nearly") { 
        print (
    "<center>You win 500 Credits!</center>"); 
    }
    else{
        print (
    "<center>You win 0 Credits!</center>"); 

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Feb 2003
    Location
    Here
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works, thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •