Debug Looping

hmmm...seem to have an issue with looping in this program.

This is the desired output:

Please enter your name: Guido
How many numbers would you like to use in playing the game (4-10)? 5
Choose a difficulty level (1-3)? Easy
        Incorrect entry: Please re-enter
Choose a difficulty level (1-3)? 2
A 5-digit number has been chosen. Each possible digit may be the number 1, 2, 3, or 4.
        * * * * *
Enter your guess (10 guesses remaining):
Digit 1: 1
Digit 2: 2
Digit 3: 3
Digit 4: 4
Digit 5: 5
        Incorrect entry: Please re-enter
Digit 5: 4
Your Guess: 1 2 3 4 4
Results:    1 Hits, 4 Misses
Enter your guess (9 guesses remaining):
Digit 1: 1
Digit 2: 1
Digit 3: 1
Digit 4: 1
Digit 5: 1
Your Guess: 1 1 1 1 1
Results:    3 Hits, 2 Misses
Enter your guess (8 guesses remaining):
Digit 1: 3
Digit 2: 3
Digit 3: 3
Digit 4: 3
Digit 5: 3
Your Guess: 3 3 3 3 3
Results:    0 Hits, 5 Misses
Enter your guess (7 guesses remaining):
Digit 1: 1
Digit 2: 1
Digit 3: 1
Digit 4: 2
Digit 5: 4
Your Guess: 1 1 1 2 4
Results:    1 Hits, 4 Misses
Enter your guess (6 guesses remaining):
Digit 1: 4
Digit 2: 2
Digit 3: 1
Digit 4: 1
Digit 5: 1
Your Guess: 4 2 1 1 1
Results:    5 Hits, 0 Misses
You win, Guido!
The correct numbers were:
4 2 1 1 1

Would you like to play again (Y/N)? n
Thank you for playing "Master-Mined"
-----------------------------------------------------------------------------------------------------



and what happens is it gets the digits for your first guess, then loops through and displays:

Your Guess: 4 2 1 1 1
Results:    5 Hits, 0 Misses

ten times in a row....


here is the code.



/* ***********************************************************************************
 *  FileName:      PeterDidowlab2.java
 *  Author:      Peter Didow
 *  Date:      September 30, 2004
 *  Purpose:      Employment Insurance
 *            
 ***********/



import java.util.Random;
import java.io.*;
import cst.io.*;      
public class Lab2
{
      /* ********************************************
       * main
       *  - Entry point of  the program
       *
       */
      

      public static void main(String[] args) throws Exception
      {
            String name;
            int number;
            char again;
        
            do
            {
                  System.out.println("Please enter your name:");
                  name = Keyboard.in();
                  playGame(name);
                  System.out.println("Would you like to play again (Y/N)?");
                  again = Keyboard.in().charAt(0);
                  
            }
            while (again == 'y' || again == 'Y');
            return;
      }
   
      private static void playGame(String name) throws Exception
      {
            int randomNumberDigits;
            int Difficulty;
            randomNumberDigits=GetRandomNumberCount();
            Difficulty=GetGameDifficulty();

            int nRemaining;//guesses
            
            nRemaining=10;


            System.out.println("A " + randomNumberDigits + " number has been chosen. Each possible digit may be the number 1, 2, 3, or 4.");
            System.out.println("\t* * * * *");

            //Generate Random Numbers
            Random generator = new Random();
            int adigit[];
            int userguess[];
            adigit= new int[randomNumberDigits];
            
            int count=0;
            for (count=0;count<randomNumberDigits;++count)
            {
                  //fill array with random numbers
                  adigit[count]=GeneratRandomNumbers(generator);
                  
            }
            switch (Difficulty)
            {
                  case 1:
                        nRemaining=10;
                        break;
                        
                  case 2:
                        nRemaining=5;
                        break;
                        
                  case 3:
                        nRemaining=3;
                        break;
            }
            //how many elements for user guess
            userguess=new int[nRemaining];

            int digitnumber=1;
            int counter=0;
            
             //While we still have guesses remaining
            
            while (nRemaining>0)
            {

                  while (digitnumber-1<randomNumberDigits) //only do this for the selected number of digits
                  {      
                        userguess[counter]=GetUserGuess(randomNumberDigits,nRemaining,digitnumber);
                        
                        counter++;
                        digitnumber++;
                  
                  }
                  
                  nRemaining--;
                  //Check if they guessed correct
                  int x=0;
                  while (x<randomNumberDigits)
                  {
                        System.out.println("Your guess:" + userguess[x] + " ");
                        x++;
                  }
      
                  //get hits misses
                  int Hits;
                  int Misses;
                  //pass in both arrays and number of digits to compare
                  Hits=CountHits(userguess,adigit,randomNumberDigits);
                  Misses=randomNumberDigits-Hits;
                  System.out.println("Results: " + Hits + "Hits," + Misses + "Misses");

                  //check if we won
                  if (Hits==randomNumberDigits)
                  {
                        System.out.println("You Win" + name);
                  }

            

            }
            
      }
      private static int GetRandomNumberCount() throws Exception
      {
            
            //get keyboard input, validate that it is numerical and in range throoug the method GetaValidNumber
            //return randomnumberDigits
            
            boolean bValid;
            bValid=false;
            
            while (bValid==false)
            {
            System.out.println("How many numbers would you like to use in playing the game (4-10)?");
            String input = Keyboard.in();
            String type=new String("GetRandomNumberCount");
                  bValid = GetVaildNumber(input,type);
            
                  if (bValid==true)
                  {
                        String strChar = String.valueOf(input);
                        int mynumber;
                        
                              mynumber = Integer.parseInt(strChar);
                              return mynumber;
                        

                  }
                  else
                  {
                        System.out.println("\tIncorrect entry: Please re-enter");  
                  }
            }
               
 return 0;
      }
   
   
      private static int GetGameDifficulty() throws Exception
      {
            boolean bValid;
            bValid=false;
            while (bValid==false)
            {
                  System.out.println("Select a Game Difficulty (1-3)?");
                  //get keyboard input, validate that it is numerical and in range throoug the method GetaValidNumber
                  //return Difficulty
                  String input=Keyboard.in();
                  String type=new String("GetGameDifficulty");
            
                  bValid = GetVaildNumber(input,type);
                  if (bValid==true)
                  {
                        String strChar = String.valueOf(input);
                        int mynumber;
                        
                        mynumber = Integer.parseInt(strChar);
                        return mynumber;
                  }
                  else
                  {
                        System.out.println("\tIncorrect entry: Please re-enter");  
                  }
            }
            return 0;
      }
               

private static int GetUserGuess(int digits,int nRemaining,int guessnumber) throws Exception
{
      System.out.println("Enter your guess (" + nRemaining + " guesses remaining):");
      
      char guess;
      int Myguess;
      Myguess=0;
      String myString;
      
      
      
            System.out.println("Digit " + guessnumber + ":");
            guess=Keyboard.in().charAt(0);
            myString=String.valueOf(guess);
            Myguess=Integer.parseInt(myString);
      

      return Myguess;
}
private static int GeneratRandomNumbers(Random generator) throws Exception
{
      int anumber;


      anumber= generator.nextInt(3) + 1;
      return anumber;


}
private static int CountHits(int userguess[], int adigit[], int digits) throws Exception
{
      int Hits=0;
      int Misses=0;
      int x=0;

      for (x=0;x<digits;x++)
      {
            if (userguess[x]==adigit[x])
            {
                  Hits++;
            }
      }

return Hits;

}
private static boolean GetVaildNumber(String number,String type) throws Exception
{
      boolean bValid;
      bValid=false;
      //convert char number to int

      

      String strChar = String.valueOf(number);
      int mynumber;
      try
      {
            mynumber = Integer.parseInt(strChar);
      }
      catch(NumberFormatException ex)
      {
            bValid=false;
            return bValid;

      }



      
      if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)

      {  
            //test for a integer value between 4-10
                  
                  
            bValid=true;
            return bValid;
            
      }         
      
      

            

      if (type.equals("GetGameDifficulty") && mynumber >= 1 && mynumber <= 3)
      {         //test for a integer value between 1-3
            
            
                  bValid=true;
            
      }

      
      if (type.equals("GetUserGuess") && mynumber >= 1 && mynumber <= 4)
      {         //test for a integer value between 1-4
            
            
                  bValid=true;
            
      }
      
      


      return bValid;
        
}
}
   
   
   
   
   


HELP!





pdidowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
> while (digitnumber-1<randomNumberDigits)

looks like you don't reset digitnumber

try:

digitnumber = 1;
while (digitnumber-1<randomNumberDigits)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
             digitnumber=1;
              counter=0;              
              while (digitnumber-1<randomNumberDigits) //only do this for the selected number of digits
               {    
                    userguess[counter]=GetUserGuess(randomNumberDigits,nRemaining,digitnumber);
                   
                    counter++;
                    digitnumber++;
               
               }
0
objectsCommented:
or just move the declarations inside the loop.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.