Number Guessing Game in Java Tutorial (Beginning to End)

Поделиться
HTML-код
  • Опубликовано: 6 янв 2021
  • Complete Java course: codingwithjohn.thinkific.com/...
    Full source code available here! www.codingwithjohn.com/number...
    Beginner's Java coding lesson tutorial where we'll create a Number guessing game completely from scratch, beginning to end, explaining everything along the way.
    Learn or improve your Java by watching it being coded live!
    Hey, I'm John! I'm a Lead Java Software Engineer who has been in the industry for over a decade. I love sharing what I've learned over the years in a way that's understandable.
    Let me know what else you'd like to see!
    Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
    📕 THE best book to learn Java, Effective Java by Joshua Bloch
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletrial.com/johnclean...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Phone I use for recording:
    amzn.to/3HepYJu
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com/donate/?hosted...
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    codingwithjohn.com
  • НаукаНаука

Комментарии • 79

  • @aghyadjabali2599
    @aghyadjabali2599 2 года назад +6

    I was struggling for one Weak by doing a Homework and i just randomly opend this video and found exactly what i looked for Thanks alot for the high quality Videos

  • @scottcenna6964
    @scottcenna6964 2 года назад +3

    Seriously, very well taught. I struggled with the if, else if and while statements. Thanks for making it practical

  • @pavlhp8518
    @pavlhp8518 Год назад +9

    Thank you so much ! I was really struggling with exactly this problem :) you saved my week! You definitely got a new subscriber in me :)

  • @KH-hn3nu
    @KH-hn3nu Год назад +1

    brilliant work John! Very satisfying beginner programming!

  • @nirukumari7721
    @nirukumari7721 3 года назад +8

    6:36😂😂
    The way you teaches, you truly deserves a million subs.
    AMAZING video!!! Thanks for the video🤗

    • @CodingWithJohn
      @CodingWithJohn  3 года назад +1

      Only about a million to go 🙃
      Thanks for watching!

    • @tibettenballs4962
      @tibettenballs4962 2 года назад

      Im surpised you didnt mention the high contrast his bald head shines in every video.... [he uses YT ad money to buy razors; so make sure you support the baldness by watching Advertz].

  • @Sebbesebastian
    @Sebbesebastian Год назад +1

    Love the content, very educational and interesting / fun to watch

  • @darkbluewwe7986
    @darkbluewwe7986 3 года назад +2

    He is explaining very easy that's nice , I think it's very easy for understanding 👍

  • @pendago8484
    @pendago8484 Год назад +1

    This was really fun to do. Especially because I begin to follow on after a couple of months hard work on this stuff, starting from scratch. Your channel works very well for me. Using this tutorials together with the OCA study book, the Java Head First edition, and ofcourse, Java for dummies.

  • @busrraengin
    @busrraengin 6 месяцев назад

    Thank you for the video. It was super helpful and made it easy to understand the concept.

  • @A_Squiggle1
    @A_Squiggle1 2 года назад +1

    You are a life saver! Thank you so much!

  • @et_22_wadhwana_sahil34
    @et_22_wadhwana_sahil34 Год назад

    Thanks it is really good and was very thankful for simple explanation

  • @shunkoo
    @shunkoo 3 года назад +6

    THANK YOU SOSOS MUCH , i haven’t paid attention in technology for the pass two years and this rlly saved my final project grade :) this was so much easier than i thought it would be thanks to you!

    • @theintrovert894
      @theintrovert894 2 года назад

      why people dont write java programs in functions why so

  • @Garrison86
    @Garrison86 2 года назад

    Amazing! Thanks for another awesome video

  • @poudied8826
    @poudied8826 3 года назад +10

    i feel like watching someone with 100k subscribers. nice tutorial :)

    • @CodingWithJohn
      @CodingWithJohn  3 года назад

      Thanks! I'm doing my best to work on that but have a long way to go. Sharing/subscribing/liking/commenting all help a ton, so I very much appreciate it!

    • @SharathThe1
      @SharathThe1 2 года назад

      I'm watching when he has 107k subs but still feels wrong.
      It should be higher!

  • @aman2op326
    @aman2op326 2 года назад +1

    Sir you are just awesome your way of explaining is very good
    lots of respect for you sir love from india😇

  • @baldeepsingh1047
    @baldeepsingh1047 2 года назад

    Thank u so much for the vedio !! By the way very interesting game and explained in easiest way.

  • @adrianasilveira4561
    @adrianasilveira4561 2 года назад

    Great explanation.. Thank you

  • @javalady
    @javalady 3 месяца назад

    that was a pretty cool exceprcise! thank you!! and you are so funny :)) i wish i could work with you :)

  • @user-et4mc3qu6l
    @user-et4mc3qu6l Месяц назад

    Thank You John 🫶

  • @paxisaac4401
    @paxisaac4401 Год назад

    THANK YOU SIRR, U SAVED ME BIG TIME!!!!!!!

  • @nickvolz4442
    @nickvolz4442 3 года назад +1

    Good stuff man👍 Same barber I see👌

  • @OHHS2
    @OHHS2 Год назад

    Thank you so much❤❤

  • @raghavsrivastava2016
    @raghavsrivastava2016 3 года назад

    I subscribe very less channels, specially that have less subcribers, but your hardwork and way made me hit that subscribe method. I was shocked that you have only 1k subs, it should be atleast 200k at present time. Love from India.

    • @CodingWithJohn
      @CodingWithJohn  3 года назад

      Thanks for the sub, and thanks for watching!

  • @allyimage8151
    @allyimage8151 3 года назад

    you got my sub Man

  • @Bengeljo
    @Bengeljo 4 месяца назад

    When I saw the scanner, I already saw the highlight and from the input output video I was like "He is not closing the scanner. Oh nooooooo" Did it on my write along in the if statement when you win the game and the scanner was happy :)

  • @designwithgeet7895
    @designwithgeet7895 3 года назад +1

    I come from totally different profession to coding and learn java, My channel created to related my work. Thank you so much I'm always scroll and find useful videos from you sir. Best thing I learn to close my scanner class, no one talk about it anywhere in tutorials. when i see that tiny yellow mark warning in eclipse it too much annoying to me because I like everything arranged properly and clean as my shape arts, But now not anymore . Thanks a lot keep posting. I wish you start a series of java tutorials for very beginners with codding challenge every week or two videos in week. wish you fast growth of you channel .
    Love From Dubai by an Indian

    • @CodingWithJohn
      @CodingWithJohn  3 года назад +1

      Thank you! I agree, I can't stand having warnings anywhere in my code. I like having a nice, clean workspace. Just makes you feel good.

  • @MarkDuivesteijn
    @MarkDuivesteijn 2 года назад +3

    Hi John, this video was very nice.I'm following your Java video's for beginners since I need to catch up on some Java which I used like 16 years ago and kind of forgot about. Your videos are easy to follow. I have one question for this little program. How can we prevent users from entering something else than a number between 1-100? You showcased how a big minus number goes wrong. The same will happen when entering letters or special characters. I'm a tester so I love to think about abusing the intended flow. A TDD approach for this little example would also be cool to see.

    • @amanmaan9647
      @amanmaan9647 Год назад

      We can write an if statement to prevent the input of numbers greater than 100 , and only accept input if number greater than 100

  • @brittanyewing3187
    @brittanyewing3187 Год назад

    Quick question! (love your videos, btw) I watched your exception videos, but I'm still a little confused because I'm very new to this. If you went and handled the exception in this game to make the user input be an int between 1-100, what would that look like?

  • @saivikasedara6321
    @saivikasedara6321 Год назад

    Thankyou sir we learn mainthing thankyou sir

  • @videography2399
    @videography2399 2 года назад

    thanks
    i think its too complicative but after watching this video its too easy for me

  • @dominicread5261
    @dominicread5261 2 года назад

    hey thanks a lot! got through it myself, how can I upload this game somewhere for friends and family to try it out, is it possible. cheers and thank

  • @shoaibaltaf705
    @shoaibaltaf705 3 года назад

    Thanks a lot my dear... Love from Pakistan..... 👌

  • @fredmoniquakibongani7336
    @fredmoniquakibongani7336 4 месяца назад

    Thanks for the content, please may I know what IDE you are using?

  • @engineer3381
    @engineer3381 Год назад

    Hello teacher do you have all lessons of java from being up to end I you have please send the link

  • @childy8463
    @childy8463 3 месяца назад

    How do you track the number of guesses and games played to display at the end of the game

  • @user-co6sh5dk9v
    @user-co6sh5dk9v 2 года назад

    Hello, John! Really I enjoyed your tutorials - they are small, easy to understand and always to the point. Great! But what about source code to some tutorials? Number guessing, Hangman... It's impossible to download it.

    • @CodingWithJohn
      @CodingWithJohn  Год назад

      Thanks! The source is available in the link in the description. You just have to sign up to my email list and it gives you the link to the source right away. (Don't worry I won't spam you, I actually haven't sent an email on that list in a very long time)

  • @ZeeCH_Music
    @ZeeCH_Music 4 месяца назад

    Hey John, how are you? just wondering if when you'll be going to include your cat on your videos? you've been referencing animal class and cat class always! I've always wanted to see your cat! Thanks for teaching us Java and making it easy to understand!

  • @anonuevomarkjoseph6822
    @anonuevomarkjoseph6822 2 года назад

    salamat idol

  • @triniboy8122
    @triniboy8122 6 месяцев назад

    Hi John.
    How do I end the game in 5 tries but loop infinite if the guess is outside the range to guess. Having problem with that

  • @jeanmicheloutayek9496
    @jeanmicheloutayek9496 Год назад

    How can i make it when its too high or too low ?

  • @swastikdey7498
    @swastikdey7498 Год назад

    You taught well. You're so funny

  • @reyaduran6
    @reyaduran6 7 месяцев назад

    How would I add where the player only gets 5 guesses? I can not figure this out.

  • @franciscosantos2560
    @franciscosantos2560 3 года назад +2

    Hi John, thanks for this tutorial. I would like to know how can I increment some functions like "Do you want to play again?" "Enter a whole number" (just in case the user enter an invalid response)

    • @rishabhpal9135
      @rishabhpal9135 2 года назад

      I don't know about "Do you want to play again" but If the data type is int u can only input integers

    • @LuneAgent
      @LuneAgent 2 года назад

      1) Do a while loop inside of another while loop.
      2) Catch NumberFormatException or make any custom condition in a loop until the user enters what you expect.

  • @advancedguide
    @advancedguide 2 года назад

    You can just write "sout: and press "enter"... More easily way...
    Time: 5:8

  • @jeffreeedom
    @jeffreeedom 2 года назад

    How to limit the attempts?

  • @ivanhernandez4191
    @ivanhernandez4191 Год назад

    how you make it replay the game

  • @prajwalthapa3304
    @prajwalthapa3304 2 года назад

    which application do you use

  • @CanHaktan
    @CanHaktan 11 месяцев назад

    13:46 caught me off guard XDDD

  • @csrctn3342
    @csrctn3342 3 года назад

    what could happen if instead a while loop you use a do while loop?

    • @CodingWithJohn
      @CodingWithJohn  3 года назад +2

      In this case it would work exactly the same, since it's just using while (true).
      The difference between the two types of loops is that a do while loop checks its loop condition after each iteration instead of before, so it ends up always running through the loop at least once time. Since a regular while loop checks at the beginning, it's possible that it may not go through the loop at all if the first time it checks the condition it's already false.
      But in this case,, since the condition is (true), of course it will always evaluate to true, So our while loop here will also always go through the loop at least one time.
      Even better, you can try the code change yourself and see what happens!

  • @nomisxid
    @nomisxid Год назад

    Once participated in a debugging session where a long running process lost 63 bytes every hour. Release your resources 😶

  • @wckd9115
    @wckd9115 2 года назад

    what app is that

  • @someonem8804
    @someonem8804 2 года назад

    This helped me understanding the basics of a normal guessing game. However I'm a new student to java that does online classes at a university that doesn't have any online classes. We get assigned reading and straight from reading right to a lab, which is graded with no classes whatsoever. I'm required to do a REVERSE GUESSING GAME in java, where I tell the computer if they are guessing low, high or got the correct number with a typed input that says "correct" from me. The computer if it doesn't guess correct is supposed to adjust the range of numbers again and then try another guess. This is supposed to repeat five times and then the program ends whether the computer guesses it or not. I understand what you're doing here but im totally 100% lost how to do the reverse. I dont know advance concepts but yet im supposed to create a game like this. My hair is about to fall out trying to accomplish this. I can't seem to find any related examples of this anywhere. Is there any tips for a java program like this? I learn better with a teacher, such as this video was amazing accomplishing just that but the style of just read from a book and do it yourself, especially for a project of this magnitude is just too much :(.

  • @As-Sirat
    @As-Sirat 3 года назад

    great tutorial, now my concerns are:
    if for example they entered: -1 or higher then 100 such as 101.
    what can be done to display the following:
    "you entered an invalid number"
    "please enter a valid number"
    ?
    also can I use math.random method instead of RandomNumbers ?
    also how can you guess the right number without knowing its the right number?

    • @CodingWithJohn
      @CodingWithJohn  3 года назад +4

      - You can just add an additional check to the beginning of the if statements:
      if(playerGuess < 0 || playerGuess > 100) {
      System.out.println("That is not a valid number. Please enter a number between 1 and 100");
      }
      else if (playerGuess == randomNumber) {
      System.out.println("Correct! You Win!");
      System.out.println("It took you " + tryCount + " tries");
      break;
      }
      else if(randomNumber > playerGuess) {
      System.out.println("Nope! The number is higher. Guess again.");
      }
      else {
      System.out.println("Nope! The number is lower. Guess again.");
      }
      - You can use Math.Random, but it gives you a double between 0.0 and 1.0, so you would need to do some fancy math to get a whole number from that.
      - The game tells you whether the number is higher or lower than your guess. With each guess you can get closer and closer to the real number, until you find out exactly what it is.

    • @xKingBoo
      @xKingBoo Год назад

      @@CodingWithJohn How would you have it say a message like "Please enter a number" when they enter a letter instead of a number.. Everytime I enter a letter instead of number it has an error!

  • @Xardas_
    @Xardas_ Год назад

    And then we can write the Result of the guessing game in a file using BufferedWriter :D

  • @mayur7164
    @mayur7164 9 месяцев назад

    i'm beginner. I used for loop for this number guessing game. no. of iterations give me no of tries.
    but i stucked on 'Score' it worked for single play score is showing. but i want scores to add each time user play game again.
    I used a method copied my full code there (loop) & in main method given prompt 'Do u want to play again' & if user clicks 'y' then method is called again.
    But problem is i used 'score' variable inside method (local var) How i can access or what should i change to access score in main method?
    Pasting my code below

    • @mayur7164
      @mayur7164 9 месяцев назад

      package guess;
      import java.util.Random;
      import java.util.Scanner;
      public class Number_game {
      public static void game() {
      int number;
      Random random = new Random();
      number = random.nextInt(100);
      number = number + 1;
      System.out.println(number);
      Scanner sc = new Scanner(System.in);
      for (int i = 1; i number) {
      System.out.println("Sorry, but you guess is too high.");
      System.out.println("please try to enter less value next time.");
      }
      else if (guess < number) {
      System.out.println("Sorry, but you guess is too low.");
      System.out.println("please try to enter higher value next time.");
      }
      }
      else {
      System.out.println("Out of range");
      sc.next();
      }
      }
      System.out.println("The correct number was: " + number);
      //System.out.println("Game Over");
      }
      public static void main(String[] args) {
      game();
      while (true) {
      System.out.println("Do you want to play agin?(Y/N)");
      Scanner sb = new Scanner(System.in);
      String again = sb.nextLine();
      if (again.equalsIgnoreCase("y")) {
      game();
      }
      else {
      System.out.println("Game Over");
      break;
      }
      }
      }
      }

  • @asseenontv247
    @asseenontv247 Год назад

    It's a pretty old video, so I doubt I'll get a response, but wouldn't it be better to create the playerGuess variable outside the loop and then reassign it's value within the loop instead of effectively recreating the variable every time you go through the loop? I know the garbage collector will take care of it, but this seems like a bit of a resource waste.

  • @atillagaming341
    @atillagaming341 Год назад

    Ly

  • @coolcyberlife2425
    @coolcyberlife2425 2 года назад

    Am i the Only one Who sees him as Johny Sins 😅😅

  • @ebrahimg8346
    @ebrahimg8346 2 года назад

    Why does he do that at the start? It's very unsettling

  • @usman_karatela
    @usman_karatela 2 года назад

    Hey John, Why you always make videos standing up..?🤔

  • @lyricszee9840
    @lyricszee9840 2 года назад

    mulla khush rakhee teno ustad

  • @gopalkannan4934
    @gopalkannan4934 9 месяцев назад

    How much cost your brain?😊

  • @varungaur20
    @varungaur20 Месяц назад

    now ssly tell me, who came here for oib project?