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

  • Home
  • SEARCH
  • 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 8172791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:53:46+00:00 2026-06-06T21:53:46+00:00

I’m a beginner in java programming. Now I’m doing a game called mastermind. It

  • 0

I’m a beginner in java programming. Now I’m doing a game called mastermind.

It has many conditions and I’m using numbers instead of colours.

I have no idea of how to solve this problem: I’m using if else statement to do my game but if the solution is 4444 and user input is 4444, the output answer is wrong.

In this game the first input is computer generated, but i have to test the program, so i set the number by myself, and the second input is the player input.

Here is the code i’ve done:

import javax.swing.JOptionPane;


public class asd {


    public static void main (String args[]){
        int i,j;
        int b=0 ,w=0;

    String input[] = new String[4];
    input[0] = JOptionPane.showInputDialog("input a digit");
    input[1] = JOptionPane.showInputDialog("input a digit");
    input[2] = JOptionPane.showInputDialog("input a digit");
    input[3] = JOptionPane.showInputDialog("input a digit");

     String Uinput[] = new String[4];
     Uinput[0] = JOptionPane.showInputDialog("input a digit");
     Uinput[1] = JOptionPane.showInputDialog("input a digit");
     Uinput[2] = JOptionPane.showInputDialog("input a digit");
     Uinput[3] = JOptionPane.showInputDialog("input a digit");

     for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(i<4){
            if(input[i].equals(Uinput[j])){
                if(i==j){
                    b++;
                    i++;
                    j=0;
                }else{
                    w++;


                }
            }
            }
        }
     }
     System.out.println("black = "+b+"\nwhite = "+w);

}
}
  • 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-06T21:53:47+00:00Added an answer on June 6, 2026 at 9:53 pm

    Ok, i’m going to give you some suggestions.

    First I would say either use a single String for each input (as Dave suggested) or use a char array. You don’t need an array of String’s for 4 characters.

    Your “correct” check is way too complicated. You are modifying loop variables in the loop and it makes it hard to decipher what is actually going on.

    EDIT

    I would modify the algorithm to a two phase one. The first phase, I would walk through the array and ONLY check for the right number in the right spot – this would be your black peg count.

    Then, the next phase, I would walk through the arrays again, checking for right number, wrong spot (white count). The tricky thing is handling duplicates. For example, if the actual number is “1234” and I guess “4445”, I should only get one white peg. You cannot naively check if the number exists in the actual value.

    To address this, the easiest thing I can think of is making a boolean array that keeps track of when you have already accounted for a number. So here’s what my “black” phase would look like:

    char[] actual = // Read in the real value
    char[] guess = // Read in the users guess
    
    int blackCount = 0;
    int whiteCount = 0;
    
    boolean[] isFound = new boolean[4];
    
    // Black phase
    for (int i = 0; i < 4; ++i)
    {
      if (actual[i] == guess[i])
      {
        isFound[i] = true;
        blackCount++;
      }
    }
    

    So now you add the white phase – and you only get a white peg if the number exists and it’s isFound[i] value = false.

    Hope that helps

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.