help with java assignment pls

Discussion in 'Plugins and Coding' started by Shamel, Jan 23, 2019.

  1. Shamel

    Shamel Member

    Messages:
    765
    Likes Received:
    2,173
    Trophy Points:
    229
    Shamel
    Emerald
    hey i have an assignment in my programing class but i got stuck with something. my assignment is to make a "game" where the game will generate a random number between 1-10 or 1-20 and you have to guess it. i also need to make the program say how many tries it took you to guess a number, so i need to make it count how many times the "guess" button is clicked. is it possible to do so? if yes please help me, thanks
     
    Elenahh likes this.
  2. Land

    Land Member

    Messages:
    1,405
    Likes Received:
    2,679
    Trophy Points:
    233
    WOOOOOOOOOOOOOOP
    Might be a good idea to include what languages / engines / whatever you're using (Edit: didnt read title smh)

    Anyway, best I can do without knowing what you're using to make this game:
    Code:
    var Number;
    var Guesses;
    var min = 1;
    var max = 20;
    var running = false;
    
    ON ButtonClick
    {
        if(running == false) {
            Number = random(min, max);
            running = true;
        }
       
        if(GuessedNumberByUser == Number) {
            alert("Congrats you guessed the number in " + Guesses + " tries!");
            running = false;
        } else {
            Guesses += 1;
            alert("Nope, not this number, try again");
        }
    }
    (just some semi-code, this will not work in any language)
     
    Elenahh likes this.
  3. DDaii

    DDaii Member

    Messages:
    1
    Likes Received:
    2
    Trophy Points:
    4
    Code:
    package test.gtn;
    
    import java.util.InputMismatchException;
    import java.util.Random;
    import java.util.Scanner;
    
    public class GuessTheNumber
    {
       private static int min = 1, max = 20;
       private static int number = 0;
       private static int guessed = 0;
       private static int tries = 0;
       private static boolean running = false;
    
       public static void main(String[] args)
       {
           number = new Random().nextInt(max);
           running = true;
    
           while(running)
           {
               System.out.print("Number: ");
               Scanner s = new Scanner(System.in);
    
               try
               {
                   guessed = s.nextInt();
    
                   if (guessed < min)
                   {
                       System.out.println("Please enter a number higher or equal to 1.");
                   }
                   else if (guessed > max)
                   {
                       System.out.println("Please enter a number lower or equal to 20.");
                   }
                   else
                   {
                       tries++;
    
                       if (guessed == number)
                       {
                           System.out.println("That's the correct number! It took you: " + tries + " ties!");
                           running = false;
                       }
                       else
                       {
                           System.out.println("Wrong number, try again.");
                       }
                   }
               }
               catch (InputMismatchException ime)
               {
                   System.out.println("NaN - Please enter a number.");
               }
           }
    
           System.exit(0);
       }
    }
    
    Compile and run with "java test.gtn.GuesstheNumber"
     
    Dutudy and Shamel like this.