How to create a random letter guessing game on java

by Ebak   Last Updated July 13, 2019 01:26 AM

I am trying to create a guessing game on java. The user is to guess a letter of the alphabet that the the computer has already generated. Lowercase and uppercase letters should be included, but the guess "A" is equivalent to the guess "a". I am trying to create a loop that repeatedly asks for a letter until the user guesses the computers letter. After each incorrect guess I need to tell the user whether their letter came before or after the correct letter.

I can not figure out how use the Random class with both uppercase and lowercase letters. I cant figure out how to convert it to numbers to tell if the guess is before or after.

import java.util.Random; import java.util.Scanner;

public class GuessLetter {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Random ran = new Random();

String alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

int N= alphabet.length();

char a =(alphabet.charAt(ran.nextInt(N)));

Scanner scanner=new Scanner(;

System.out.println("Enter a letter:");

String i= scanner.nextLine();

char b =(i.charAt(0));

while {

Tags : java

Answers 1

You can use String.indexOf(char) to find the position of the user's input in your set of possible characters. Then compare that position to the random number you generated to determine if it's higher or lower.

July 13, 2019 01:25 AM

Related Questions

Advice on ecommerce backend with Java

Updated January 09, 2018 04:26 AM

Java: No Such File Or Directory (Using Scanner Class)

Updated November 04, 2017 02:26 AM

How does toArray(Foo[]::new) work in Java Streams?

Updated November 17, 2018 17:26 PM