Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how can i improve this file to account for space index when user enters a letter: import java.util.Scanner; public class Hangman { private static final

how can i improve this file to account for space index when user enters a letter: import java.util.Scanner;
public class Hangman{
private static final boolean testingMode=true;
private static String level="";
private static boolean keepPlaying=true;
private static String hiddenWord="";
private static int numGuesses=0;
private static int numSpaces=0;
public static void main(String[]args){
Scanner console=new Scanner(System.in);
while(keepPlaying){
System.out.println("Enter your difficulty: Easy(e), Intermediate(i), or Hard(h)");
level=console.nextLine();
while(!level.equalsIgnoreCase("e")&&
!level.equalsIgnoreCase("i")&&
!level.equalsIgnoreCase("h")){
System.out.println("Not a valid input");
level=console.nextLine();
}
Hang(level, console);
System.out.println("Would you like to play again? Enter y or n to continue");
keepPlaying=console.nextLine().equalsIgnoreCase("y");
}
System.exit(0);
}
private static void Hang(String level, Scanner console){
String secretWord=RandomWord.newWord();
if(testingMode){
System.out.println("The secret word is: "+ secretWord);
}
if(level.equalsIgnoreCase("e")){
numGuesses=15;
numSpaces=4;
}
else if(level.equalsIgnoreCase("i")){
numGuesses=12;
numSpaces=3;
}
else if(level.equalsIgnoreCase("h")){
numGuesses=10;
numSpaces=2;
}
for(int i=0; i< secretWord.length();i++){
hiddenWord+="-";
}
System.out.println("The word is: "+ hiddenWord);
while(!hiddenWord.equals(secretWord)){
System.out.print("Please enter the letter you want to guess: ");
String guess=console.nextLine();
System.out.println("Please enter the spaces you want to check (seperated by spaces): ");
numSpaces=console.nextInt();
console.nextLine();
if(guess.length()>1||!Character.isLetter(guess.charAt(0))){
System.out.println("Your input is not valid. Try again");
System.out.println("Guesses remaining: "+ numGuesses);
}
else{
char let=guess.charAt(0);
for (int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions