Programming Project help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Programming Project help

mandyhelpmandyhelp Posts: 1Member
My assignment is to create a program that is a variation of the game mastermind. This is what I have so far, but something is not working right. I would appreciate any help. Thanks.

import java.util.*;
public class gilroy {

/**
* @param args
*/
public static void main(String[] args) {




Random rand = new Random();

Scanner console = new Scanner(System.in);

int n = 4;
System.out.println("Please enter a four-digit number:");
int entry = console.nextInt();

int[] guess = new int[n];
for (int i = n - 1; i >= 0; i--) {
guess[i] = entry % 10;
entry = entry / 10;
}

System.out.println("Guess Array Contains " + java.util.Arrays.toString(guess));


int[] nCode = new int[n];
for (int j = 0; j < n; j++) {
nCode[j] = rand.nextInt(10);
}

System.out.println("nCode Array Contains " + java.util.Arrays.toString(nCode));

do {

Check(nCode, guess, n);
System.out.println("Enter your next guess: ");
guess = new int[console.nextInt()];

} while (!nCode.equals(guess));

System.out.println("You Win!");

}

private static void Check(int[] a, int[] b, int length) {

int rightSpot = 0;
int wrongSpot = 0;
int i = 0;
int j = 0;

for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {

int tempa = a[i];
int tempb = b[j];
if (tempa == tempb && i == j) {

rightSpot++;

} else if (tempa == tempb && i != j) {

wrongSpot++;

}
else if (a.equals(b)) {
System.out.println("That answer is correct!");
rightSpot = length;
wrongSpot = 0;
}
}
}

System.out.println(rightSpot + " of your digits are correct and in the "
+ "correct spot.");
System.out.println(wrongSpot + " of your digits are correct but in the "
+ "incorrect spot.");
}
{


}

}

Sign In or Register to comment.