Number Guessing Game in Java Tutorial (Beginning to End)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2024
  • Complete Java course: codingwithjohn...
    Full source code available here! www.codingwith...
    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.audibletria...
    🖥️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...
    ☕Complete Java course:
    codingwithjohn...
    codingwithjohn...

ความคิดเห็น • 80

  • @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

  • @nirukumari7721
    @nirukumari7721 3 ปีที่แล้ว +9

    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].

  • @scottcenna6964
    @scottcenna6964 2 ปีที่แล้ว +3

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

  • @poudied8826
    @poudied8826 3 ปีที่แล้ว +11

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

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว +1

      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 ปีที่แล้ว +2

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

  • @pavlhp8518
    @pavlhp8518 2 ปีที่แล้ว +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 :)

  • @Littleseth69420
    @Littleseth69420 หลายเดือนก่อน

    Thank you so much for doing this in eclipse! I've been learning code for a week online through Eclipse but every tutorial for this project uses Visual code, i spent 20 minutes trying to configure it but the messy output me off immediately you're a life saver

  • @ibeshun
    @ibeshun 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

  • @darkbluewwe7986
    @darkbluewwe7986 3 ปีที่แล้ว +2

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

  • @raghavsrivastava2016
    @raghavsrivastava2016 3 ปีที่แล้ว +1

    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!

  • @KH-hn3nu
    @KH-hn3nu ปีที่แล้ว +1

    brilliant work John! Very satisfying beginner programming!

  • @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

  • @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😇

  • @Bengeljo
    @Bengeljo 6 หลายเดือนก่อน

    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 :)

  • @pendago8484
    @pendago8484 2 ปีที่แล้ว +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 9 หลายเดือนก่อน

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

  • @Sebbesebastian
    @Sebbesebastian ปีที่แล้ว +1

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

  • @A_Squiggle1
    @A_Squiggle1 3 ปีที่แล้ว +1

    You are a life saver! Thank you so much!

  • @nick_7979
    @nick_7979 3 ปีที่แล้ว +1

    Good stuff man👍 Same barber I see👌

  • @DillipKumar-q8q
    @DillipKumar-q8q 3 หลายเดือนก่อน

    Thank You John 🫶

  • @et_22_wadhwana_sahil34
    @et_22_wadhwana_sahil34 ปีที่แล้ว

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

  • @javalady
    @javalady 5 หลายเดือนก่อน

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

  • @ZeeCH_Music
    @ZeeCH_Music 7 หลายเดือนก่อน

    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!

  • @CanHaktan
    @CanHaktan ปีที่แล้ว +1

    13:46 caught me off guard XDDD

  • @paxisaac4401
    @paxisaac4401 2 ปีที่แล้ว

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

  • @saivikasedara6321
    @saivikasedara6321 ปีที่แล้ว

    Thankyou sir we learn mainthing thankyou sir

  • @baldeepsingh1047
    @baldeepsingh1047 2 ปีที่แล้ว

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

  • @fredmoniquakibongani7336
    @fredmoniquakibongani7336 6 หลายเดือนก่อน

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

  • @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.

  • @videography2399
    @videography2399 2 ปีที่แล้ว

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

  • @Garrison86
    @Garrison86 2 ปีที่แล้ว

    Amazing! Thanks for another awesome video

  • @OHHS2
    @OHHS2 ปีที่แล้ว

    Thank you so much❤❤

  • @adrianasilveira4561
    @adrianasilveira4561 2 ปีที่แล้ว

    Great explanation.. Thank you

  • @allyimage8151
    @allyimage8151 3 ปีที่แล้ว

    you got my sub Man

  • @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?

  • @АлександрПудовкин-р6т
    @АлександрПудовкин-р6т 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  2 ปีที่แล้ว

      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)

  • @childy8463
    @childy8463 5 หลายเดือนก่อน

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

  • @reyaduran6
    @reyaduran6 10 หลายเดือนก่อน

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

  • @swastikdey7498
    @swastikdey7498 ปีที่แล้ว

    You taught well. You're so funny

  • @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

  • @triniboy8122
    @triniboy8122 8 หลายเดือนก่อน

    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

  • @shoaibaltaf705
    @shoaibaltaf705 3 ปีที่แล้ว

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

  • @engineer3381
    @engineer3381 2 ปีที่แล้ว

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

  • @advancedguide
    @advancedguide 2 ปีที่แล้ว

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

  • @nomisxid
    @nomisxid ปีที่แล้ว

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

  • @jeanmicheloutayek9496
    @jeanmicheloutayek9496 ปีที่แล้ว

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

  • @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.

  • @mayur7164
    @mayur7164 ปีที่แล้ว

    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 ปีที่แล้ว

      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;
      }
      }
      }
      }

  • @prajwalthapa3304
    @prajwalthapa3304 2 ปีที่แล้ว

    which application do you use

  • @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 :(.

  • @anonuevomarkjoseph6822
    @anonuevomarkjoseph6822 2 ปีที่แล้ว

    salamat idol

  • @jeffreeedom
    @jeffreeedom 2 ปีที่แล้ว

    How to limit the attempts?

  • @Xardas_
    @Xardas_ ปีที่แล้ว

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

  • @ivanhernandez4191
    @ivanhernandez4191 ปีที่แล้ว

    how you make it replay the game

  • @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!

  • @usman_karatela
    @usman_karatela 2 ปีที่แล้ว

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

  • @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!

  • @wckd9115
    @wckd9115 2 ปีที่แล้ว

    what app is that

  • @ebrahimg8346
    @ebrahimg8346 2 ปีที่แล้ว

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

  • @coolcyberlife2425
    @coolcyberlife2425 2 ปีที่แล้ว

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

  • @atillagaming341
    @atillagaming341 ปีที่แล้ว

    Ly

  • @gopalkannan4934
    @gopalkannan4934 11 หลายเดือนก่อน

    How much cost your brain?😊

  • @lyricszee9840
    @lyricszee9840 3 ปีที่แล้ว

    mulla khush rakhee teno ustad

  • @varungaur20
    @varungaur20 3 หลายเดือนก่อน

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