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
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].
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!
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
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!
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
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.
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.
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 :)
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)
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.
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.
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
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); } }
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?
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!
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.
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)
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!
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?
- 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.
@@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!
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 :(.
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
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; } } } }
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.
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
6:36😂😂
The way you teaches, you truly deserves a million subs.
AMAZING video!!! Thanks for the video🤗
Only about a million to go 🙃
Thanks for watching!
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].
Seriously, very well taught. I struggled with the if, else if and while statements. Thanks for making it practical
Thank you so much ! I was really struggling with exactly this problem :) you saved my week! You definitely got a new subscriber in me :)
i feel like watching someone with 100k subscribers. nice tutorial :)
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!
I'm watching when he has 107k subs but still feels wrong.
It should be higher!
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
brilliant work John! Very satisfying beginner programming!
He is explaining very easy that's nice , I think it's very easy for understanding 👍
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!
why people dont write java programs in functions why so
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
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.
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.
Thanks for the sub, and thanks for watching!
Thank you for the video. It was super helpful and made it easy to understand the concept.
Thanks it is really good and was very thankful for simple explanation
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.
We can write an if statement to prevent the input of numbers greater than 100 , and only accept input if number greater than 100
You are a life saver! Thank you so much!
Love the content, very educational and interesting / fun to watch
THANK YOU SIRR, U SAVED ME BIG TIME!!!!!!!
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 :)
Sir you are just awesome your way of explaining is very good
lots of respect for you sir love from india😇
Thank You John 🫶
thanks
i think its too complicative but after watching this video its too easy for me
13:46 caught me off guard XDDD
Good stuff man👍 Same barber I see👌
Thanks for the content, please may I know what IDE you are using?
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)
I don't know about "Do you want to play again" but If the data type is int u can only input integers
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.
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.
Hello teacher do you have all lessons of java from being up to end I you have please send the link
Thank u so much for the vedio !! By the way very interesting game and explained in easiest way.
Amazing! Thanks for another awesome video
Great explanation.. Thank you
Thankyou sir we learn mainthing thankyou sir
you got my sub Man
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
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);
}
}
that was a pretty cool exceprcise! thank you!! and you are so funny :)) i wish i could work with you :)
Thank you so much❤❤
You can just write "sout: and press "enter"... More easily way...
Time: 5:8
How do you track the number of guesses and games played to display at the end of the game
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?
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
which application do you use
How can i make it when its too high or too low ?
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!
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
How to limit the attempts?
what app is that
How would I add where the player only gets 5 guesses? I can not figure this out.
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.
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)
Thanks a lot my dear... Love from Pakistan..... 👌
how you make it replay the game
what could happen if instead a while loop you use a do while loop?
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!
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?
- 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.
@@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!
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 :(.
I feel like we're on the same course lol
You taught well. You're so funny
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
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;
}
}
}
}
And then we can write the Result of the guessing game in a file using BufferedWriter :D
salamat idol
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.
Once participated in a debugging session where a long running process lost 63 bytes every hour. Release your resources 😶
Hey John, Why you always make videos standing up..?🤔
Why does he do that at the start? It's very unsettling
Am i the Only one Who sees him as Johny Sins 😅😅
Ly
How much cost your brain?😊
mulla khush rakhee teno ustad
now ssly tell me, who came here for oib project?