Number Guessing Game in Java Tutorial (Beginning to End)

  Рет қаралды 63,300

Coding with John

Coding with John

Күн бұрын

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

Пікірлер: 82
@aghyadjabali2599
@aghyadjabali2599 2 жыл бұрын
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 жыл бұрын
6:36😂😂 The way you teaches, you truly deserves a million subs. AMAZING video!!! Thanks for the video🤗
@CodingWithJohn
@CodingWithJohn 3 жыл бұрын
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 жыл бұрын
Seriously, very well taught. I struggled with the if, else if and while statements. Thanks for making it practical
@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
@pavlhp8518
@pavlhp8518 2 жыл бұрын
Thank you so much ! I was really struggling with exactly this problem :) you saved my week! You definitely got a new subscriber in me :)
@ibeshun
@ibeshun 3 жыл бұрын
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
@poudied8826
@poudied8826 3 жыл бұрын
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!
@KH-hn3nu
@KH-hn3nu Жыл бұрын
brilliant work John! Very satisfying beginner programming!
@darkbluewwe7986
@darkbluewwe7986 3 жыл бұрын
He is explaining very easy that's nice , I think it's very easy for understanding 👍
@designwithgeet7895
@designwithgeet7895 3 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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
@busrraengin
@busrraengin 9 ай бұрын
Thank you for the video. It was super helpful and made it easy to understand the concept.
@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!
@DillipKumar-q8q
@DillipKumar-q8q 4 ай бұрын
Thank You John 🫶
@aman2op326
@aman2op326 2 жыл бұрын
Sir you are just awesome your way of explaining is very good lots of respect for you sir love from india😇
@Onespica
@Onespica 8 күн бұрын
I actually tried coding mine online before continuing with your video. Im gonna see how you did yours! Edit: Your code was much more simpler to understand! I overcomplicated mine lol
@Onespica
@Onespica 8 күн бұрын
My attempt: import java.util.Scanner; class HelloWorld { public static void main(String[] args) { int rand = (int)(Math.random() * 100); System.out.println("For debugging, Number is: "+rand); int userGuess; int wrongGuess=0; Scanner userInput = new Scanner(System.in); do{ System.out.println("Tries left: " + (10 - wrongGuess)); System.out.print("Enter your number guess (1-100): "); userGuess = userInput.nextInt(); if (wrongGuess == 9){ System.out.println("You lose! The number was " + rand+"!"); break; } if (rand == userGuess){ System.out.println("You win!"); break; } else if (userGuess < rand){ System.out.println("The number is higher. Try again!"); wrongGuess++; } else { System.out.println("The number is lower. Try again!"); wrongGuess++; } } while (wrongGuess < 10); } }
@et_22_wadhwana_sahil34
@et_22_wadhwana_sahil34 2 жыл бұрын
Thanks it is really good and was very thankful for simple explanation
@pendago8484
@pendago8484 2 жыл бұрын
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.
@A_Squiggle1
@A_Squiggle1 3 жыл бұрын
You are a life saver! Thank you so much!
@Bengeljo
@Bengeljo 7 ай бұрын
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 :)
@Sebbesebastian
@Sebbesebastian 2 жыл бұрын
Love the content, very educational and interesting / fun to watch
@fredmoniquakibongani7336
@fredmoniquakibongani7336 7 ай бұрын
Thanks for the content, please may I know what IDE you are using?
@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?
@paxisaac4401
@paxisaac4401 2 жыл бұрын
THANK YOU SIRR, U SAVED ME BIG TIME!!!!!!!
@adrianasilveira4561
@adrianasilveira4561 2 жыл бұрын
Great explanation.. Thank you
@franciscosantos2560
@franciscosantos2560 3 жыл бұрын
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.
@javalady
@javalady 6 ай бұрын
that was a pretty cool exceprcise! thank you!! and you are so funny :)) i wish i could work with you :)
@CanHaktan
@CanHaktan Жыл бұрын
13:46 caught me off guard XDDD
@baldeepsingh1047
@baldeepsingh1047 2 жыл бұрын
Thank u so much for the vedio !! By the way very interesting game and explained in easiest way.
@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!
@OHHS2
@OHHS2 Жыл бұрын
Thank you so much❤❤
@childy8463
@childy8463 6 ай бұрын
How do you track the number of guesses and games played to display at the end of the game
@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
@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
@saivikasedara6321
@saivikasedara6321 Жыл бұрын
Thankyou sir we learn mainthing thankyou sir
@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
@videography2399
@videography2399 2 жыл бұрын
thanks i think its too complicative but after watching this video its too easy for me
@nick_7979
@nick_7979 3 жыл бұрын
Good stuff man👍 Same barber I see👌
@Garrison86
@Garrison86 2 жыл бұрын
Amazing! Thanks for another awesome video
@АлександрПудовкин-р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)
@reyaduran6
@reyaduran6 10 ай бұрын
How would I add where the player only gets 5 guesses? I can not figure this out.
@allyimage8151
@allyimage8151 3 жыл бұрын
you got my sub Man
@prajwalthapa3304
@prajwalthapa3304 2 жыл бұрын
which application do you use
@jeanmicheloutayek9496
@jeanmicheloutayek9496 Жыл бұрын
How can i make it when its too high or too low ?
@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 :(.
@yoyochante-yo7306
@yoyochante-yo7306 Жыл бұрын
I feel like we're on the same course lol
@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 жыл бұрын
- 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
@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; } } } }
@nomisxid
@nomisxid Жыл бұрын
Once participated in a debugging session where a long running process lost 63 bytes every hour. Release your resources 😶
@jeffreeedom
@jeffreeedom 2 жыл бұрын
How to limit the attempts?
@ivanhernandez4191
@ivanhernandez4191 2 жыл бұрын
how you make it replay the game
@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.
@shoaibaltaf705
@shoaibaltaf705 3 жыл бұрын
Thanks a lot my dear... Love from Pakistan..... 👌
@anonuevomarkjoseph6822
@anonuevomarkjoseph6822 2 жыл бұрын
salamat idol
@csrctn3342
@csrctn3342 3 жыл бұрын
what could happen if instead a while loop you use a do while loop?
@CodingWithJohn
@CodingWithJohn 3 жыл бұрын
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!
@Xardas_
@Xardas_ Жыл бұрын
And then we can write the Result of the guessing game in a file using BufferedWriter :D
@swastikdey7498
@swastikdey7498 Жыл бұрын
You taught well. You're so funny
@usman_karatela
@usman_karatela 2 жыл бұрын
Hey John, Why you always make videos standing up..?🤔
@ebrahimg8346
@ebrahimg8346 2 жыл бұрын
Why does he do that at the start? It's very unsettling
@atillagaming341
@atillagaming341 Жыл бұрын
Ly
@coolcyberlife2425
@coolcyberlife2425 2 жыл бұрын
Am i the Only one Who sees him as Johny Sins 😅😅
@lyricszee9840
@lyricszee9840 3 жыл бұрын
mulla khush rakhee teno ustad
@gopalkannan4934
@gopalkannan4934 11 ай бұрын
How much cost your brain?😊
@varungaur20
@varungaur20 4 ай бұрын
now ssly tell me, who came here for oib project?
Create Rock Paper Scissors in Java in 10 Minutes
10:24
Coding with John
Рет қаралды 118 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1,1 МЛН
Всё пошло не по плану 😮
00:36
Miracle
Рет қаралды 3,4 МЛН
This dad wins Halloween! 🎃💀
01:00
Justin Flom
Рет қаралды 12 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 41 МЛН
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 756 М.
Create a Sudoku Solver In Java In 20 Minutes - Full Tutorial
20:25
Coding with John
Рет қаралды 323 М.
LinkedList vs ArrayList in Java Tutorial - Which Should You Use?
11:43
Coding with John
Рет қаралды 599 М.
Tic Tac Toe Game in Java - Full Tutorial with Source
51:39
Coding with John
Рет қаралды 74 М.
Bubble Sort Algorithm Tutorial in Java - How Fast Is It?
11:33
Coding with John
Рет қаралды 73 М.
Map and HashMap in Java - Full Tutorial
10:10
Coding with John
Рет қаралды 587 М.
Optionals In Java - Simple Tutorial
15:53
Coding with John
Рет қаралды 216 М.
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 191 М.
Learn Java in One Video - 15-minute Crash Course
14:54
Coding with John
Рет қаралды 744 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 83 М.
Всё пошло не по плану 😮
00:36
Miracle
Рет қаралды 3,4 МЛН