Building a Guessing Game | Java | Tutorial 20

preview_player
Показать описание

Throughout the course we'll be looking at various topics including variables, arrays, getting user input, loops, conditionals, object orientation, and much more.
Рекомендации по теме
Комментарии
Автор

Been on and off coding for years. You've literally made me understand everything i couldn't pick up as well so easily :)

nathansmart
Автор

thank you ! saved me a boat load of time on my homework assignment! Keep it up!

geenade
Автор

Good example of a beginner game to program.

angieg
Автор

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!");
}

}

Future
Автор

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:

{
if(guessCount < guessLimit) {
System.out.print("Enter a Guess: ");
guess = keyboardInput.nextLine();
guessCount++;
}else {
outOfGuesses = true;
System.out.println("You lose :P");
break;
}
}
{
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!!

Kickflips
Автор

oh man!!! you are
so easy to understand....
thanks for explanation

milandave
Автор

I did it slightly differently. I put the if statement outside and after the loop, It's looking for if the guessCount <= guessLimit and if the guess.equals(secretWord)
This makes it so if guessLimit is 4 you have for attempts to try.

thelawgameplaywithcommenta
Автор

What if i want to create 2 options:
1.with no limited attempts
2.with limited attempts

????

immer
Автор

The only place I look for Computer Project solutions😁😁😁😁

dishabanerjee_chair
Автор

for the guess limit, could you also put the while loop inside a for loop that is = to the guess limit?

halaramimido
Автор

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)

zoilavaca
Автор

THANK YOU VERY MUCH!!! my grades have hope now, because of you!

hakirawr
Автор

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

chifuyu
Автор

wow amazing example, thanks so much!!!!

norbertkulacin
Автор

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

joshuatruelen
Автор

how to add point for giving right answer and deduct point for wrong guess

salonipatel
Автор

Thank you so much for these exercises 💪

kishorkumar-uelj
Автор

You just did my homework for me 🤧🤧❤️❤️❤️❤️thank you

flashmash
Автор

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 ? ";

String guess="carmit";
String SecretWord=input.nextLine();
int i=3;
&& 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");
}
}

LTProduction
Автор

he wrote 30line code and he said this was a lot of code in the program ? lol

tanasisyanev
visit shbcf.ru