Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:41:19+00:00 2026-06-15T09:41:19+00:00

Right now for a Java course I’m trying to build a Mastermind-like game. In

  • 0

Right now for a Java course I’m trying to build a Mastermind-like game. In this game a 4-digit random number is generated, and the user tries to guess the number. With each guess the computer states how many correct digits are in the right order, and how many correct digits are in the wrong order.

For some reason, everything works up to my binary search for this program, which is really the heart of the program. I’ve spent hours tweaking it and I still cant get it. Any ideas?

In this example I’m trying to guess 9935, I realize that’s not a random number though.

Thanks so much!

EDIT: When I run this program and use the guess “9875”, it does not give me the right results.

The guesses and results I’m required to find are:

Please enter a four-digit number: 9874

The number of correct digits but in the wrong place: 0

The number of correct digits in the right place: 1

Please enter a four-digit number: 9899

The number of correct digits but in the wrong place: 1

The number of correct digits in the right place: 1

Please enter a four-digit number: 9593

The number of correct digits but in the wrong place: 3

The number of correct digits in the right place: 1

Please enter a four-digit number: 9935

The number of correct digits but in the wrong place: 0

The number of correct digits in the right place: 4

You are correct!

public class Mastermind {

public static void main(String[] args) {

    Random randomGenerator = new Random();
    Scanner input = new Scanner(System.in);

    int randomNumber = 9935;

    int[] randomArray = new int[4];

    int temp = randomNumber;
    for (int i = 3; i >= 0; i--){
        int n = temp%10;
        randomArray[i] = n;
        temp /= 10;
    }

    boolean found = false;

    while (found == false){

        System.out.print(Arrays.toString(randomArray));
        int[] guessArray = new int[4];
        System.out.print("Please enter a four-digit number: ");
        int guessTemp = input.nextInt();
        for (int i = 3; i >= 0; i--){
            int n = guessTemp%10;
            guessArray[i] = n;
            guessTemp /= 10;
        }

        if (Arrays.equals(randomArray, guessArray)){
            System.out.println("You are correct!");
            found = true;
        } else {

            int numberRightRight = 0;
            int numberRightWrong = 0;
            int indexFound = 0;
            for(int i = 0; i < guessArray.length; i ++){
                System.out.println(randomArray[i]);
                indexFound = Arrays.binarySearch(guessArray, randomArray[i]);
                System.out.println(indexFound);
                if (indexFound >= 0){
                    if(indexFound == i){
                        numberRightRight++;
                    } else {
                        numberRightWrong++;
                    }
                }
            }
           System.out.println("The number of correct digits but in the wrong place: " + numberRightWrong);
           System.out.println("The number of correct digits in the right place: " + numberRightRight);
        }   
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:41:20+00:00Added an answer on June 15, 2026 at 9:41 am

    If you are not required to use Arrays.binarySearch(int[], int) you could use your own simple lookup method for an unsorted Array:

    public static int findInArray(int[] array, int value) {
      for (int i = 0; i < array.length; i++) {
        if (array[i] == value) {
            return i;
        }
      }
      return -1;
    }
    

    now instead of calling Arrays.binarySearch(guessArray, randomArray[i]); just call findInArray(guessArray, randomArray[i])

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I have my code looking like this: SqlTables.java: // Separate file public
I'm trying to learn Java right now and I've jumped in the deep end
Right now, I'm writing a small java application by my own, with few maven
We are using a Java EE application and we are right now using Informix
I am coming from Java and try to learn Javascript right now. Doing that
Basically I am java developer and exploring ruby and ruby related framework right now.
It doesn't seem to be working right now. I get a java.lang.NullPointerException I have
Ok I'm kinda in a predicament right now. I have a java program that
Right now when I run this it keeps clicking on the same button every
I'm moving from Java to C++ right now and I'm having some difficulties whenever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.