I paused the video before you set up the "lose" condition to see if I could figure it out, it works but I came up with something different and I'd like to know if there is any practical difference. While it functions the same, is the way I have it less efficient or does it matter? Here's where it differs: while(!guess.equals(secretWord)) { if(guessCount < guessLimit) { System.out.print("Enter a Guess: "); guess = keyboardInput.nextLine(); guessCount++; }else { outOfGuesses = true; System.out.println("You lose :P"); break; } } if(guess.equals(secretWord)) { System.out.println("You Win!"); } P.S. This is my first time coding and your teaching style has made it so easy to grasp thank you!!
I made some minor changes to the code so that the user guesses are not cap-sensitive and I decided to not used booleans as it confused me but the user still gets 3 guesses public static void main(String[] args) { Scanner input = new Scanner(System.in); String secretWord = "cowboys"; String guess = ""; int guessCount = 0; int guessLimit = 3; while (!guess.equals(secretWord)) { if (guessCount < guessLimit) { System.out.print("Enter a guess: "); guess = input.nextLine(); guess = guess.toLowerCase(); guessCount++; if (guessCount == guessLimit) { System.out.println("Out of guesses, You lose!"); } } } System.out.println("You Win!"); } }
How can I add a list of words so that the "secret word"grabs a word from that list , and how can I validate that the user only uses strings when they are guessing ? Thanks from a noob 8)
The list I want to add is in a .txt file - my guess is to import it? because there are too many words to make an array... Hope to hear from you soon! :)
@@zoilavaca7015 The game here is a very simple game... And that's the point of this tutorial. It's for explaining the concept of loop using a simple game. But if you still want to do it anyway, what you should do is: 1. Read a .TXT file and convert it into an ArrayList of words 2. Set the secretWord to a random word from the ArrayList
I made a different code but with same basics and here it is: import java.util.Scanner; public class game{ public static void main (String[]args){ Scanner sc = new Scanner (System.in); String SecretWord ="giraffe"; String guess=""; int count=0; while(!guess.equals(SecretWord)) { System.out.println("Enter a guess"); guess= sc.nextLine(); count++; if(guess.equals(SecretWord)){ System.out.println("You Win!!"); }else if(count==3){ System.out.println("It is an animal"); }else if(count==6) { System.out.println("This animal lives in forest"); }else if(count==8){ System.out.println("Hey loser!"); break; } } } }
great - this is my code :) import java.util.*; public class Guessing_Game { public static Scanner input=new Scanner(System.in); public static void main(String[] args) { String question="SHE HAS BROWN HAIR & CURLLY HAIR, BROWN EYES,WHO IS SHE ? "; System.out.println(question); String guess="carmit"; String SecretWord=input.nextLine(); int i=3; while(!guess.equals(SecretWord) && i>=0) {
System.out.println("You have another "+i+" guesses to guess"); System.out.println("try again"); SecretWord=input.next(); i--; } if(i>=0) { System.out.println("you are right");} else System.out.println("you lose"); } }
Been on and off coding for years. You've literally made me understand everything i couldn't pick up as well so easily :)
BROOOOO thank you ! saved me a boat load of time on my homework assignment! Keep it up!
How r u now?
Good example of a beginner game to program.
I paused the video before you set up the "lose" condition to see if I could figure it out, it works but I came up with something different and I'd like to know if there is any practical difference. While it functions the same, is the way I have it less efficient or does it matter? Here's where it differs:
while(!guess.equals(secretWord)) {
if(guessCount < guessLimit) {
System.out.print("Enter a Guess: ");
guess = keyboardInput.nextLine();
guessCount++;
}else {
outOfGuesses = true;
System.out.println("You lose :P");
break;
}
}
if(guess.equals(secretWord)) {
System.out.println("You Win!");
}
P.S.
This is my first time coding and your teaching style has made it so easy to grasp thank you!!
oh man!!! you are genius........
so easy to understand....
thanks for explanation
I did it slightly differently. I put the if statement outside and after the loop, It's looking for if the guessCount
I made some minor changes to the code so that the user guesses are not cap-sensitive and I decided to not used booleans as it confused me but the user still gets 3 guesses
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String secretWord = "cowboys";
String guess = "";
int guessCount = 0;
int guessLimit = 3;
while (!guess.equals(secretWord)) {
if (guessCount < guessLimit) {
System.out.print("Enter a guess: ");
guess = input.nextLine();
guess = guess.toLowerCase();
guessCount++;
if (guessCount == guessLimit) {
System.out.println("Out of guesses, You lose!");
}
}
}
System.out.println("You Win!");
}
}
THANK YOU VERY MUCH!!! my grades have hope now, because of you!
for the guess limit, could you also put the while loop inside a for loop that is = to the guess limit?
The only place I look for Computer Project solutions😁😁😁😁
What if i want to create 2 options:
1.with no limited attempts
2.with limited attempts
????
wow amazing example, thanks so much!!!!
Awesome explanation! Thanks
Really great content, THANK YOU!
Simple and easy to understand. Thanks
How can I add a list of words so that the "secret word"grabs a word from that list , and how can I validate that the user only uses strings when they are guessing ? Thanks from a noob 8)
The list I want to add is in a .txt file - my guess is to import it? because there are too many words to make an array... Hope to hear from you soon! :)
@@zoilavaca7015 The game here is a very simple game... And that's the point of this tutorial. It's for explaining the concept of loop using a simple game. But if you still want to do it anyway, what you should do is:
1. Read a .TXT file and convert it into an ArrayList of words
2. Set the secretWord to a random word from the ArrayList
How could you add total score? For example you make 5 questions and it takes correct answer and it print (you got 3 out of 5) etc....
Make a int variable score, and each time the user gets a correct answer increase it by one... Then at last print the score
I made a different code but with same basics and here it is:
import java.util.Scanner;
public class game{
public static void main (String[]args){
Scanner sc = new Scanner (System.in);
String SecretWord ="giraffe";
String guess="";
int count=0;
while(!guess.equals(SecretWord)) {
System.out.println("Enter a guess");
guess= sc.nextLine();
count++;
if(guess.equals(SecretWord)){
System.out.println("You Win!!");
}else if(count==3){
System.out.println("It is an animal");
}else if(count==6) {
System.out.println("This animal lives in forest");
}else if(count==8){
System.out.println("Hey loser!");
break;
}
}
}
}
why won't it work for me?
what application is this?
how to add point for giving right answer and deduct point for wrong guess
Why didn’t you import random ?
Thank you so much for these exercises 💪
I just heard a dog barking Lol 7:48
urs ears are really so sharp!!
coming from c++ basics, this is hell
You are a genius
Thanks a lot!!!
It was so useful
How can i add hints?
Thank you so much
it says some are error
Oh Thanks! :)
Thanks IDOL
You just did my homework for me 🤧🤧❤️❤️❤️❤️thank you
great - this is my code :)
import java.util.*;
public class Guessing_Game {
public static Scanner input=new Scanner(System.in);
public static void main(String[] args) {
String question="SHE HAS BROWN HAIR & CURLLY HAIR, BROWN EYES,WHO IS SHE ? ";
System.out.println(question);
String guess="carmit";
String SecretWord=input.nextLine();
int i=3;
while(!guess.equals(SecretWord) && i>=0) {
System.out.println("You have another "+i+" guesses to guess");
System.out.println("try again");
SecretWord=input.next();
i--;
}
if(i>=0) {
System.out.println("you are right");}
else
System.out.println("you lose");
}
}
he wrote 30line code and he said this was a lot of code in the program ? lol
Well this is for beginners and this vid has the most code written in it so far of all the tutorials so yeah