Hi guys I have this code. It works, but it has a minor glitch which i cannot troubleshoot. Please find the code below followed by an image of my output.
import java.util.Random; import java.util.Scanner; public class letters { public static void main(String[] args){ Random rn = new Random(); Scanner sc = new Scanner(System.in); //generation of a random letter char letter = (char)(rn.nextInt(26) + 'A'); System.out.println("******************"+letter); char alphabet; char startingLetter = 'A'; char endingLetter = 'Z'; char guessedLetter = '.'; int i = 0; //the purpose of this (do, while) loop is for the program to loop while the gussedLetter is incorrect. do { //Calling the method generateRange to generate the alphabet generateRange(startingLetter, endingLetter); //Requesting user input System.out.println(""); System.out.println("Please Choose a Letter from above"); System.out.println(""); System.out.print("Selection: "); guessedLetter = sc.nextLine().charAt(0); //calling the validation method to validate the character inputted guessedLetter = validation(guessedLetter); //this conditional statement will alter the endingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified endingLetter. //The condition will also tell the user that the possible answer is to the left if(letter < guessedLetter) { System.out.println(""); System.out.println("Incorrect -- its to the Left of " + guessedLetter+". Your Letters are between: "); endingLetter = guessedLetter; } //this conditional statement will alter the startingLetter of the alphabet. Once the program loops again it will call the method generateRange again, and the program will display the characters with the modified startingLetter. //The condition will also tell the user that the possible answer is to the right. else if (letter > guessedLetter){ System.out.println(""); System.out.println("Incorrect -- its to the Right of " + guessedLetter+". Your Letters are between: "); startingLetter = guessedLetter; } //this counter will keep track of the number of tries it took the user to win! i++; } while (letter != guessedLetter); //Outputs when the answer is correct. and tells the user the number of tries. System.out.println(""); System.out.println("Correct -- You managed in " + i + " tries"); } //the purpose of this method is to generate the alphabet according to the startingLetter and endingLetter which get modified according to the users guesses public static void generateRange(char start, char end){ char alphabet; for(alphabet = start; alphabet <= end; alphabet++ ) { System.out.print(alphabet+" "); } System.out.println(""); } //the purpose of this method is to validate the characters inputted. The program will ask the user to input his guess again if a wrong character is entered, example a number of a symbol. //the switch will also convert any lowercase letters to uppercase allowing the user to enter both lower or upercase letters public static char guessedLetter = '.'; public static char validation(char letterValid) { switch(letterValid) { case 'A': case 'a': guessedLetter = 'A'; break; case 'B': case 'b': guessedLetter = 'B'; break; case 'C': case 'c': guessedLetter = 'C'; break; case 'D': case 'd': guessedLetter = 'D'; break; case 'E': case 'e': guessedLetter = 'E'; break; case 'F': case 'f': guessedLetter = 'F'; break; case 'G': case 'g': guessedLetter = 'G'; break; case 'H': case 'h': guessedLetter = 'H'; break; case 'I' : case 'i': guessedLetter = 'I'; break; case 'J' : case 'j': guessedLetter = 'J'; break; case 'K': case 'k': guessedLetter = 'K'; break; case 'L' : case 'l': guessedLetter = 'L'; break; case 'M' : case 'm': guessedLetter = 'M'; break; case 'N' : case 'n': guessedLetter = 'N'; break; case 'O' : case 'o': guessedLetter = 'O'; break; case 'P' : case 'p': guessedLetter = 'P'; break; case 'Q' : case 'q': guessedLetter = 'Q'; break; case 'R' : case 'r': guessedLetter = 'R'; break; case 'S' : case 's': guessedLetter = 'S'; break; case 'T' : case 't': guessedLetter = 'T'; break; case 'U' : case 'u': guessedLetter = 'U'; break; case 'V' : case 'v': guessedLetter = 'V'; break; case 'W' : case 'w': guessedLetter = 'W'; break; case 'X' : case 'x': guessedLetter = 'X'; break; case 'Y' : case 'y': guessedLetter = 'Y'; break; case 'Z' : case 'z': guessedLetter = 'Z'; break; default : { //will run if anything apart from A - Z is entered. System.out.println(""); System.out.println("Invalid -- Please Choose a Letter!"); System.out.println(""); System.out.print("Selection: "); Scanner sc = new Scanner(System.in); guessedLetter = sc.nextLine().charAt(0); validation(guessedLetter); } } return guessedLetter; } }
This is my output:
https://dl.dropboxusercontent.com/u/6055003/Capture.PNG
The problem I have is, for example, when I chose R. The code showed R included in the generated alphabet. After it says that it should be left from R. I need to remove that R.
Thanks in advance!