Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mastermind program help

    Hello all
    I've been working on a Mastermind program for a while now and I seem to be getting a problem when I run it.

    Methods

    Code:
      public void casualcode()
    {
    
        do
        {
          boomhauer[variable1] = (int)(Math.random() * 6) + 1;
          variable1++;
        }
        while (variable1 <= 3);
        
        for(int bob = 0; bob <= 3; bob++)
    {
    System.out.println(boomhauer[bob]);
    }
    if (variable1 == 1) {
       hank[variable2] = "Red";
      } else if (variable1 == 2) {
       hank[variable2] = "Blue";
      } else if (variable1 == 3) {
       hank[variable2] = "Green";
      } else if (variable1 == 4) {
       hank[variable2] = "Brown";
      } else if (variable1 == 5) {
       hank[variable2] = "Yellow";
      } else if (variable1 == 6) {
       hank[variable2] = "White";
      }
      
      
      for(int carl = 0; carl <= 3; carl++)
      {
        System.out.println(hank[carl]);
     }    
    
    
    
    
    
    }
    }
    Runner/Main

    Code:
    import java.io.*;
    
    public class Mastermindrunner
    {
      public static void main(String[] args) 
      {
         String guile; String honda; String viper; int juri; String mature; char yoko;
        Mastermind hadoken = new Mastermind();
        guile = hadoken.firstinput();
        honda = hadoken.helpmenu();
        viper = hadoken.difficulty();
         hadoken.casualcode();
        hadoken.casualintro();
        
     
      }
    }

    As you can see, the code is VERY incomplete at the moment, the only difficulty setting that works at the moment is Casual mode.
    But anywho, that's not the point.
    See this part of the methods?
    Code:
    public void casualcode()
    {
    
        do
        {
          boomhauer[variable1] = (int)(Math.random() * 6) + 1;
          variable1++;
        }
        while (variable1 <= 3);
        
        for(int bob = 0; bob <= 3; bob++)
    {
    System.out.println(boomhauer[bob]);
    }
    if (variable1 == 1) {
       hank[variable2] = "Red";
      } else if (variable1 == 2) {
       hank[variable2] = "Blue";
      } else if (variable1 == 3) {
       hank[variable2] = "Green";
      } else if (variable1 == 4) {
       hank[variable2] = "Brown";
      } else if (variable1 == 5) {
       hank[variable2] = "Yellow";
      } else if (variable1 == 6) {
       hank[variable2] = "White";
      }
      
      for(int cool = 0; cool <= 3; cool++)
      {
        System.out.println(hank[cool]);
      }
      }
    }
    What SHOULD be happening is this..
    An array of 4 random numbers is generated
    THEN another array of 4 strings is generated
    The strings will correspond with the random number array (ie if one of the random numbers is 4 then the array will print out Brown)
    However when I run the program this is what I get
    4
    1
    1
    4
    Brown
    null
    null
    null
    Does anyone know the reason as to why it prints out as null and only Brown prints out?
    I would greatly appreciate any help.
    (try to ignore the goofy object and variable names)

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,478
    Thanks
    6
    Thanked 981 Times in 954 Posts
    Java and JavaScript are two completely different languages. Please keep that in mind for the future. Moved to correct forum.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    This is the JavaSCRIPT forum. About the only thing that JAVA (your code) and JavaScript have in common are the first 4 letters of their names.

    But before you post in the right forum, some advice:

    NOBODY could figure out FOR SURE from the code you have shown what your problem is.

    You never show how variable2 is created and/or what values it might have.

    But even having said that, your code makes no sense. And I *think* I see what is happening.

    You say "and array of 4 random numbers is generated". Yes, I see that. In the array named boomhauer.

    You then say "THEN another array of 4 strings is generated". No, that is not true.

    You only ever set the value of *ONE* of the hank[..] strings.

    There is NO LOOP in the code that does the hank[variable2] = "..." stuff. So ONLY ONE of those array entries will be given a string value. The rest WILL be null.

    And we know what the value of variable1 will be when that is done: It will *ALWAYS* be 4! Because you ran this code:
    Code:
        do
        {
          boomhauer[variable1] = (int)(Math.random() * 6) + 1;
          variable1++;
        }
        while (variable1 <= 3);
    and at the end of it, variable1 will *ALWAYS* have a value of 4.

    So when you get to the series of "if" conditions, the ONLY one that will be executed is this one:
    Code:
      } else if (variable1 == 4) {
       hank[variable2] = "Brown";
    So one and only one of the elements of the hank[] array will have the string "Brown".

    And since you NEVER INITIALIZE OR CHANGE the value of variable2, that means that doing
    Code:
       hank[variable2] = "Brown";
    is actually doing
    Code:
       hank[0] = "Brown";
    And THAT is why you always get "Brown" as the one and only string that is displayed.

    **********

    In short, throw all that code away and start over. It's utter trash.

    Code:
        String[] colors = {"Red","Blue","Green","Brown","Yellow","White"};
        for ( int i = 0; i <= 3; ++i )
        {
            int rnd = (int)(Math.random() * 6);
            boomhauer[i] = rnd + 1;
            hank[i] = colors[rnd];
            System.out.println(hank[i]); // to demo that it worked
        }
    There. That just did the same thing your code purported to do, in one heluva lot less code.

    ******

    EDIT: I see this thread has been moved to the Java forum. Good.
    Last edited by Old Pedant; 12-04-2011 at 10:45 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welp
    I feel like an idiot
    A thousand thanks Old Pendant
    Also sorry for the forum mix up.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •