-1

I get an infinite loop every time I input the numbers 1, 3, 5 and I don't know how to fix it. This is a mastermind program using arrays in which pegs are used instead of numbers. The problem is most likely in the while loop in which was changed to make the program more condensed. Any help would be amazing.

package New;
import java.util.Random;

import New.ArrayMethod2;
import TurtleGraphics.KeyboardReader;

public class Mastermind2 {



    public static void main(String[] args) {
        Mastermind2 object = new Mastermind2();KeyboardReader reader = new KeyboardReader();
    int[] V = new int [3];
    do {

    System.out.println("Please choose your first number (1-5)");
    V[0]=reader.readInt();
    System.out.println("Please choose your second number (1-5)");   
    V[1]=reader.readInt();
    System.out.println("Please choose your third number (1-5)");
    V[2]=reader.readInt();
    }while((V[0]>5)&&(V[1]>5)&&(V[2]>5));
    object.CorrectVariables(V[0], V[1], V[2]);
}
public void CorrectVariables(int num1, int num2, int num3) {
    Mastermind1 object = new Mastermind1();
    int Cornum1, Cornum2, Cornum3;
    Random generator = new Random();
    Cornum1 = generator.nextInt(5)+1;
    Cornum2 = generator.nextInt(5)+1;
    Cornum3 = generator.nextInt(5)+1;
    IFstatements(Cornum1, Cornum2, Cornum3, num1, num2, num3);
}
public void IFstatements(int Cornum1, int Cornum2, int Cornum3, int num1, int num2, int num3) {
    Mastermind1 object = new Mastermind1();
    do{
        int Number=0, Color=0;
        if(Cornum1==num1)
        {
            Number++;
            Color++;
        }
        if(Cornum1==num2)
        {
            Color++;
        }
        if(Cornum1==num3)
        {
            Color++;
        }
        if(Cornum2==num2)
        {
            Number++;
            Color++;
        }
        if(Cornum2==num1)
        {
            Color++;
        }
        if(Cornum2==num3)
        {
            Color++;
        }
        if(Cornum3==num3)
        {
            Number++;
            Color++;
        }
        if(Cornum3==num1)
        {
            Color++;
        }
        if(Cornum3==num2)
        {
            Color++;
        }
        System.out.println("You have "+Number+" numbers correct and "+Color+" colors correct");
        if((Cornum1!=num1)||(Cornum2!=num2)||(Cornum3!=num3));
        {
            Number=0;
            Color=0;
        }
    }while((Cornum1!=num1)&&(Cornum2!=num2)&&(Cornum3!=num3));
    System.out.println("Congrats you guessed the correct numbers");
}

}

Print out:

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Andreas Oct 23 '20 at 16:44
  • [What is a **debugger** and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Oct 23 '20 at 16:44
  • 1
    Your loop body doesn't change any of the 6 parameter variables used in the `(Cornum1!=num1)&&(Cornum2!=num2)&&(Cornum3!=num3)` condition, so if the method is called with values that aren't equal to begin with, why would you expect looping to change that? – Andreas Oct 23 '20 at 16:47
  • Please paste your output into the question as text. As an image, it is too small to read. – NomadMaker Oct 23 '20 at 18:17
  • One suggestion: if you change your program, keep track of the changes so that you can understand how the changes work. – NomadMaker Oct 23 '20 at 18:18

2 Answers2

0

It looks like the exit condition for your IFStatements function is that the user's input matches the correct pattern. However, the user does not get a chance to update his input and you never change the num or Cornum variables to match the correct output. Therefore, the loop is going to continue to execute forever. What you need to do is have that code run once and then give the user a chance to update their answers.

Fox5
  • 1
0
public static void main(String[] args) {
    Mastermind2 object = new Mastermind2();
    Scanner scan = new Scanner(System.in);
    int one = getNumber("first", scan);
    int two = getNumber("second", scan);
    int three = getNumber("third", scan);
    object.correctVariables(one, two, three);
}

private static int getNumber(String str, Scanner scan) {
    while (true) {
        System.out.print("Please choose your " + str + " number (1-5): ");
        int num = scan.nextInt();

        if (num >= 1 && num <= 5)
            return num;
    }
}

public void correctVariables(int one, int two, int three) {
    Random random = new Random();
    int expectedOne = random.nextInt(5) + 1;
    int expectedTwo = random.nextInt(5) + 1;
    int expectedThree = random.nextInt(5) + 1;
    ifStatements(expectedOne, expectedTwo, expectedThree, one, two, three);
}

public void ifStatements(int expectedOne, int expectedTwo, int expectedThree, int one, int two, int three) {
    int num = expectedOne == one ? 1 : 0;
    num += expectedTwo == two ? 1 : 0;
    num += expectedThree == three ? 1 : 0;

    int color = expectedOne == one ? 1 : 0;
    color += expectedOne == two ? 1 : 0;
    color += expectedOne == three ? 1 : 0;

    color += expectedTwo == one ? 1 : 0;
    color += expectedTwo == two ? 1 : 0;
    color += expectedTwo == three ? 1 : 0;

    color += expectedThree == one ? 1 : 0;
    color += expectedThree == two ? 1 : 0;
    color += expectedThree == three ? 1 : 0;

    System.out.println("You have " + num + " numbers correct and " + color + " colors correct");

    if (num == 3)
        System.out.println("Congrats you guessed the correct numbers");
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35