Another java question - please help!
Posted: Thu Nov 04, 2004 7:47 pm
Okay here is the deal, another project for my Java class... It's due tomorrow at midnight and I have been sitting here trying to get this thing working but no luck. Here is the problem, hopefully I am missing something simple :
The assignment is to make it so that the solutioncode for the game mastermind is created randomly at the start of the project. So, I want to create a 4-index character array that is randomized, with the possibilities being R G B Y W K for Red, Green, Blue, Yellow, White or Black, respectively.
The instructions say this:
* public static char[] makeCode(int numberOfPegs) - creates and returns a new solution sequence based on the number of pegs provided as a parameter.
Here is the snippet of code that I added to my program. It seemed to be working by itself in a test file but when I put it into the main code I've been doing over the semester it doesn't work.
When I try to compile I get the error "cannot resolve symbol : variable solution" This occurs when I try to compare the solution to pegs. wtf? I am doing something wrong here.
Please help!
The assignment is to make it so that the solutioncode for the game mastermind is created randomly at the start of the project. So, I want to create a 4-index character array that is randomized, with the possibilities being R G B Y W K for Red, Green, Blue, Yellow, White or Black, respectively.
The instructions say this:
* public static char[] makeCode(int numberOfPegs) - creates and returns a new solution sequence based on the number of pegs provided as a parameter.
Here is the snippet of code that I added to my program. It seemed to be working by itself in a test file but when I put it into the main code I've been doing over the semester it doesn't work.
Code: Select all
class P3 {
int numberOfPegs = 4;
public static char[] makeCode(int numberOfPegs){
char[] solution = new char[numberOfPegs];
int i = 0;
for(i = 0; i < solution.length; i++){
int r = (int)(Math.random()*6);
if(r == 0){
solution[i] = 'R';
}else if (r == 1){
solution[i] = 'G';
}else if (r == 2){
solution[i] = 'B';
}else if (r == 3){
solution[i] = 'Y';
}else if (r == 4){
solution[i] = 'W';
}else if (r == 5){
solution[i] = 'B';
}
}//close for loop
}//close makeCode
Please help!