Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2011, 09:48 PM   PM User | #1
DPXJube
New to the CF scene

 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
DPXJube is an unknown quantity at this point
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
Quote:
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)
DPXJube is offline   Reply With Quote
Old 12-04-2011, 10:28 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,596
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Java and JavaScript are two completely different languages. Please keep that in mind for the future. Moved to correct forum.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 12-04-2011, 10:43 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.

Last edited by Old Pedant; 12-04-2011 at 10:45 PM..
Old Pedant is offline   Reply With Quote
Old 12-04-2011, 11:22 PM   PM User | #4
DPXJube
New to the CF scene

 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
DPXJube is an unknown quantity at this point
Welp
I feel like an idiot
A thousand thanks Old Pendant
Also sorry for the forum mix up.
DPXJube is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 PM.


Advertisement
Log in to turn off these ads.