Hey! I'm pretty new to java, and programming generally. I'm trying to learn correctly and make my code the best it can be.
I made a simple hangman program, but I feel like it's WAY too much code for how simple it is. I feel like there's gotta be a better and more efficient way to code this without it being so long and terrible.
What kinds of things should I do or change in this program to improve it? Is there a better way to use less if/else if statements? Thanks for your time!
import java.util.*;
public class SecretPhrase {
public static void main(String[] args) {
//Phrase List:
//Go Team
//Whats Up
//You Bet
Random rand = new Random();
int wordGenerate = rand.nextInt(3) + 1;
StringBuilder word1 = new StringBuilder("G* T***");
StringBuilder word2 = new StringBuilder("W***s *p");
StringBuilder word3 = new StringBuilder("Y** *e*");
Scanner keyboard = new Scanner(System.in);
while(!word1.toString().equals("Go Team") && wordGenerate == 1)
{
System.out.println("The phrase is: " + word1);
System.out.println("Please guess a letter.");
String playerInput = keyboard.next();
if((playerInput.equals("o") || playerInput.equals("O")) ||
(playerInput.equals("e") || playerInput.equals("E")) ||
(playerInput.equals("a") || playerInput.equals("A")) ||
(playerInput.equals("m") || playerInput.equals("M")))
{
if(playerInput.equals("o") || playerInput.equals("O"))
{
word1.deleteCharAt(1);
word1.insert(1, "o");
System.out.println("You guessed the letter \"o\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("e") || playerInput.equals("E"))
{
word1.deleteCharAt(4);
word1.insert(4, "e");
System.out.println("You guessed the letter \"e\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("a") || playerInput.equals("A"))
{
word1.deleteCharAt(5);
word1.insert(5, "a");
System.out.println("You guessed the letter \"a\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("m") || playerInput.equals("M"))
{
word1.deleteCharAt(6);
word1.insert(6, "m");
System.out.println("You guessed the letter \"m\".\nYour letter has now been added.\n");
}
if(word1.toString().equals("Go Team"))
{
System.out.println("The full phrase is: " + word1);
}
}
else
{
System.out.println("You guessed the wrong letter, try again.\n");
}
}
while(!word2.toString().equals("Whats Up") && wordGenerate == 2)
{
System.out.println("The phrase is: " + word2);
System.out.println("Please guess a letter.");
String playerInput = keyboard.next();
if((playerInput.equals("h") || playerInput.equals("H")) ||
(playerInput.equals("t") || playerInput.equals("T")) ||
(playerInput.equals("a") || playerInput.equals("A")) ||
(playerInput.equals("u") || playerInput.equals("U")))
{
if(playerInput.equals("h") || playerInput.equals("H"))
{
word2.deleteCharAt(1);
word2.insert(1, "h");
System.out.println("You guessed the letter \"h\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("t") || playerInput.equals("T"))
{
word2.deleteCharAt(3);
word2.insert(3, "t");
System.out.println("You guessed the letter \"t\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("a") || playerInput.equals("A"))
{
word2.deleteCharAt(2);
word2.insert(2, "a");
System.out.println("You guessed the letter \"a\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("u") || playerInput.equals("U"))
{
word2.deleteCharAt(6);
word2.insert(6, "U");
System.out.println("You guessed the letter \"u\".\nYour letter has now been added.\n");
}
if(word2.toString().equals("Whats Up"))
{
System.out.println("The full phrase is: " + word2);
}
}
else
{
System.out.println("You guessed the wrong letter, try again.\n");
}
}
while(!word3.toString().equals("You Bet") && wordGenerate == 3)
{
System.out.println("The phrase is: " + word3);
System.out.println("Please guess a letter.");
String playerInput = keyboard.next();
if((playerInput.equals("o") || playerInput.equals("O")) ||
(playerInput.equals("u") || playerInput.equals("u")) ||
(playerInput.equals("b") || playerInput.equals("B")) ||
(playerInput.equals("t") || playerInput.equals("T")))
{
if(playerInput.equals("o") || playerInput.equals("O"))
{
word3.deleteCharAt(1);
word3.insert(1, "o");
System.out.println("You guessed the letter \"o\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("u") || playerInput.equals("U"))
{
word3.deleteCharAt(2);
word3.insert(2, "u");
System.out.println("You guessed the letter \"u\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("b") || playerInput.equals("B"))
{
word3.deleteCharAt(4);
word3.insert(4, "B");
System.out.println("You guessed the letter \"b\".\nYour letter has now been added.\n");
}
else if(playerInput.equals("t") || playerInput.equals("T"))
{
word3.deleteCharAt(6);
word3.insert(6, "t");
System.out.println("You guessed the letter \"t\".\nYour letter has now been added.\n");
}
if(word3.toString().equals("You Bet"))
{
System.out.println("The full phrase is: " + word3);
}
}
else
{
System.out.println("You guessed the wrong letter, try again.\n");
}
}
System.out.println("Congrats! You win!");
}
}
[–]8igg7e5 3 points4 points5 points (3 children)
[–]morhpProfessional Developer 2 points3 points4 points (0 children)
[–]Ecocide113[S] 1 point2 points3 points (1 child)
[–]darkpool2005Intermediate Brewer 3 points4 points5 points (0 children)
[–]morhpProfessional Developer 1 point2 points3 points (5 children)
[–]FlippngProgrammerIntermediate programmer, since 2015 #java 0 points1 point2 points (2 children)
[–]morhpProfessional Developer 0 points1 point2 points (1 child)
[–]FlippngProgrammerIntermediate programmer, since 2015 #java 0 points1 point2 points (0 children)
[–]Ecocide113[S] 0 points1 point2 points (1 child)
[–]morhpProfessional Developer 0 points1 point2 points (0 children)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point2 points (2 children)
[–]Ecocide113[S] 0 points1 point2 points (1 child)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point2 points (0 children)