Reputation: 71
This program is a guessing game in which you input a letter between a and j and try to guess the randomly chosen answer. It works correctly, but I want to use a for loop to encompass all three guesses instead of writing them all out separately. Any suggestions on how to start this?
import java.util.Scanner;
import java.lang.Math;
import java.util.Random;
class Guess{
public static void main( String[] args ){
Scanner sc = new Scanner(System.in);
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
for(int i = 0; i<3; i++){ //this is where I'm having trouble
System.out.print("Enter your first guess: ");
Random r = new Random();
char i = (char)(r.nextInt(10) + 'a');
char b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
System.out.print("Enter your second guess: ");
b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
System.out.print("Enter your third guess: ");
b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
}
if(b!=i){
System.out.println("You lose. The letter was " +i+ ".");
}
}
}
Upvotes: 1
Views: 129
Reputation: 211
Store the character the user should guess in a variable outside the loop.
Then inside the loop ask them to attempt guess number i+1 where i is the loop counter (or you can change to using a 1 based index).
If execution continues after the loop then it means they lost.
Here is an example:
import java.util.Random;
import java.util.Scanner;
class Guess {
public static void main(String[] args) {
int maxGuesses = 3;
System.out.println("I'm thinking of a letter in the range a to j. You have " + maxGuesses + " guesses. ");
Random r = new Random();
char i = (char) (r.nextInt(10) + 'a');
Scanner sc = new Scanner(System.in);
String[] guessDescription = { "First", "Second", "Third" };
for (int g = 0; g < maxGuesses; g++) {
// use predefined guess description for 1-3, otherwise a generic description that works for any number of guesses above 3
if (g < guessDescription.length) {
System.out.print("Enter your " + guessDescription[g] + " guess:");
} else {
System.out.print("Enter guess #" + (g + 1) + ":");
}
char b = sc.next().charAt(0);
if (b > i) {
System.out.println("Your guess is too high. ");
} else if (b < i) {
System.out.println("Your guess is too low. ");
} else if (b == i) {
System.out.println("You win! ");
return;
}
}
System.out.println("You lose. The letter was " + i + ".");
}
}
Upvotes: 2
Reputation:
Here is a quick example with a loop to play again if the game is over
class Guess {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
char toGuess;
char yourGuess;
boolean playing = true;
boolean lost = true;
while(playing) {
toGuess = (char) (r.nextInt(10) + 'a');
System.out.println("NEW GAME");
System.out.println("========");
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
for (int j = 0; j < 3; j++) {
switch (j) {
case 0:
System.out.print("Enter your first guess: ");
break;
case 1:
System.out.print("Enter your second guess: ");
break;
case 2:
System.out.print("Enter your third guess: ");
break;
default:
System.out.println("Something went wrong. Game is closing!");
System.exit(0);
break;
}
yourGuess = sc.next().charAt(0);
if (yourGuess > toGuess) {
System.out.println("Your guess is too high.");
} else if (yourGuess < toGuess) {
System.out.println("Your guess is too low. ");
} else if (yourGuess == toGuess) {
System.out.println("You win! ");
lost = false;
break;
}
}
if(lost==false){
System.out.println("Sorry, you lost!");
}
System.out.print("Do you want to play again? (y=yes | n=no): ");
if(!sc.next().equals("y")){
playing=false;
}
}
}
}
Upvotes: 0
Reputation: 777
You just need to replace your line Enter your .... guess.
Based on i, you should keep a mapping like
{ 0:First,1:Second,2:Third}
Scanner sc = new Scanner(System.in);
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
List < String > strList = new ArrayList <String> ( );
strList.add("FIRST");
strList.add("SECOND");
strList.add("THIRD");
Random r = new Random();
char randX = (char)(r.nextInt(10) + 'a');
for(int i = 0; i<3; i++){ //this is where I'm having trouble
System.out.print("Enter your"+ strList.get(i)+ " guess: ");
char b = sc.next().charAt(0);
if(b==i){
System.out.println("You win! ");
return;
}
else
{
if(b>randX)
{
System.out.println("Your guess is too high. ");
}
else
System.out.println("Your guess is too low. ");
if ( b !=randX && i==2 )
{
System.out.println("You lose. The letter was " + randX + ".");
}
}
}
Upvotes: 0
Reputation: 16
Map your letters into a List<> or ArrayList<> then use for-each loop
Check this answer for a simple example of how to use for-each loop:
Upvotes: 0
Reputation: 81
you can use a nested loop. this part is basically the same, and can be in inner loop (of 3 iterations)
System.out.print("Enter your ... guess: ")
char b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
but the System.out.print("Enter your ... guess: ")
is different every iteration. You can create const array of string with the strings {"first", "second", "third"} and access the correct string by the index of the loop (use format string here...)
Upvotes: 0