Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Debug Looping

Posted on 2004-11-14
3
Medium Priority
?
227 Views
Last Modified: 2010-03-31
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!





0
Comment
Question by:pdidow
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12580808
> while (digitnumber-1<randomNumberDigits)

looks like you don't reset digitnumber

try:

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

Expert Comment

by:ozo
ID: 12580812
             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
 
LVL 92

Expert Comment

by:objects
ID: 12580822
or just move the declarations inside the loop.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question