Quantcast
Viewing all articles
Browse latest Browse all 2703

Programming Project help

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.");
}
{


}

}


Viewing all articles
Browse latest Browse all 2703

Trending Articles