In this program, there is a code that the user has to try to solve in 10 guesses. If you have any numbers in the right spot, it will tell you how many you have correct and the value of the correct numbers so you can figure it out. I know there has to be a much simpler way to do this, I just can't put one together. I could use arrays (although i'm not the best with them yet) to find certain values, but i'm not positive on how to do that. Any tips? (I apologize in advance for any sloppy coding or poor choice of variable names. Also this is in a main method, i just didn't copy the class name or the main method)
String code = "53492";
int guesses = 10;
Scanner s12 = new Scanner(System.in);
System.out.println("Try to guess the 5 number code. You have 10 guesses: ");
String guess = s12.nextLine();
int right = 0;
int combo = 0; //correct values combined
int correctInt = 0;
for(int i = 0; i < 10; i++) //will allow you to guess a certain amount of times
{
combo = 0;
right = 0;
for(int x = 0; x < 5; x++) //starts searching at first spot(0)
{
int guessInt = Integer.parseInt(guess);
int codeInt = Integer.parseInt(code);
String getCorrectInt = guess.substring(x, x + 1);
correctInt = Integer.parseInt(getCorrectInt); //stores value of num at the value of
String getCodeGuess = code.substring(x, x + 1);
int codeGuess = Integer.parseInt(getCodeGuess);
if(guessInt == codeInt)
{
System.out.println("Correct! You guessed it!");
System.exit(0);
}
if(correctInt == codeGuess) //if the num at the position matches the code at the right position
{
combo += correctInt; //totals up the correct amount of numbers in right spot
right++; //keeps track of how many are right
}
}
guesses--;
System.out.println("You have " + right + " number(s) correct that equal to " + combo + " and you have " + guesses + " guesses left.");
System.out.println("Guess again: ");
guess = s12.nextLine();
}
System.out.println("You ran out of guesses! The code was " + code);