Rock Paper Scissors in Java (I just want to know whatever feedback you have :) Thank you!) Any feedback on this code would be greatly appreciated! I am just trying to improve so I can move on to bigger projects. Thank you so much!

    import java.util.*;
   import java.util.Random;

   public class RockPaperScissors {
     public static void main(String [] args)
     {


    Random rd = new Random();
    Scanner kb = new Scanner(System.in);
    System.out.println("Welcome to Rock Paper Scisscors!");

    while(true)
    {
    int rock = 1;
    int paper = 2;
    int scissors = 3;

    int conditions [] = {rock, paper, scissors};
    int random = (conditions[new Random().nextInt(conditions.length)]);

    System.out.print("Please Enter: \n1 for Rock, \n2 for Paper, \n3 for Scissors: ");
    int player1 = kb.nextInt();



    if(player1 == rock &amp;&amp; random == rock)
    {
        System.out.print("You have tied, computer rock, please try again\n");
    }
    else if(player1 == paper &amp;&amp; random == paper)
    {
        System.out.print("You have tied, computer chose paper, please try again");
    }
    else if(player1 == scissors &amp;&amp; random == scissors)
    {
        System.out.print("You have tied, computer chose scissors, please try again");
    }

    if(player1 == rock &amp;&amp; random == paper)
    {
        System.out.print("Computer chose paper, computer has won");
        break;
    }
    else if(player1 == paper &amp;&amp; random == rock)
    {
        System.out.print("You win!, computer chose rock");
        break;
    }
    else if(player1 == paper &amp;&amp; random == scissors)
    {
        System.out.print("Computer chose scissors, computer has won");
        break;
    }
    else if(player1 == scissors &amp;&amp; random == paper)
    {
        System.out.print("You win, computer chose paper");
        break;
    }
    else if(player1 == rock &amp;&amp; random == scissors)
    {
        System.out.print("You win!, computer chose scissors");
        break;
    }
    else if(player1 == scissors &amp;&amp; random == rock)
    {
        System.out.print("Computer chose rock, computer has won");
        break;
    }


    if(player1 != rock &amp;&amp; player1 != paper &amp;&amp; player1 != scissors )
    {
        System.out.print("Invalid response");
        break;
    }
    }

     }



   }
I have some suggestions for you.

Next time, before submitting code, try to format it :)

Suggestions

Unused variable

The variable rd is not used, you can remove it.

Put variables that don’t change out of the loop

In the while loop, the rock, paper and scissors get their value reassigned with the same value each time the loop restart, move them outside of the loop.

I suggest that you put those as class constants, so you will be able to reuse the in other methods.

public class RockPaperScissors {
    public static final int ROCK = 1;
    public static final int PAPER = 2;
    public static final int SCISSORS = 3;
}


Validate the invalid choice first

In your code, you check if the chosen option is valid at the end of the loop; it will be more efficient if you check the invalid options first. This will prevent to check every other conditions and quit the loop faster.

System.out.print("Please Enter: \n1 for Rock, \n2 for Paper, \n3 for Scissors: ");
int player1 = kb.nextInt();

if (player1 != rock &amp;&amp; player1 != paper &amp;&amp; player1 != scissors) {
   System.out.print("Invalid response");
   break;
}


Extract some of the logic into methods

In your code, you can move some of the logic into method to allow the code to be shorter and easier to read.


I suggest make methods to check if the answer is a rock, paper or scissors; this will allow the conditions to be shorter.


public static void main(String[] args) {
   while (true) {
      //[...]
      if (isRock(player1) &amp;&amp; isRock(random)) {
         //[...]
      }
      //[...]
   }
}

private static boolean isRock(int answer) {
   return answer == ROCK;
}

private static boolean isPaper(int answer) {
   return answer == PAPER;
}

private static boolean isScissor(int answer) {
   return answer == SCISSORS;
}



I suggest that you extract the logic that read the value in a method. 


public static void main(String[] args) {
   while (true) {
      //[...]
      int player1 = getPlayer1Answer(kb);
   }
}

private static int getPlayer1Answer(Scanner kb) {
   System.out.print("Please Enter: \n1 for Rock, \n2 for Paper, \n3 for Scissors: ");
   return kb.nextInt();
}


Bonus

While you are in the method, I suggest to make another loop to verify the validity of the answer; this will make sure the provided choice is valid and ask again if not.

private static int getPlayer1Answer(Scanner scanner) {
   int value;

   while (true) {
      System.out.print("Please Enter: \n1 for Rock, \n2 for Paper, \n3 for Scissors: ");
      value = scanner.nextInt();

      if (isRock(value) || isPaper(value) || isScissor(value)) {
         break;
      } else {
         System.out.println("Invalid response");
      }
   }

   return value;
}



I suggest that you extract the logic that generates the computer answer in a method. 


    private static int getComputerAnswer() {
        int[] conditions = {ROCK, PAPER, SCISSORS};
        return conditions[new Random().nextInt(conditions.length)];
    }


Refactored code

public class RockPaperScissors {
    public static final int ROCK = 1;
    public static final int PAPER = 2;
    public static final int SCISSORS = 3;

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        System.out.println("Welcome to Rock Paper Scissors!");

        while (true) {
            int random = getComputerAnswer();
            int player1 = getPlayer1Answer(kb);

            if (isRock(player1) &amp;&amp; isRock(random)) {
                System.out.print("You have tied, computer rock, please try again\n");
            } else if (isPaper(player1) &amp;&amp; isPaper(random)) {
                System.out.print("You have tied, computer chose paper, please try again");
            } else if (isScissor(player1) &amp;&amp; isScissor(random)) {
                System.out.print("You have tied, computer chose scissors, please try again");
            }

            if (isRock(player1) &amp;&amp; isPaper(random)) {
                System.out.print("Computer chose paper, computer has won");
                break;
            } else if (isPaper(player1) &amp;&amp; isRock(random)) {
                System.out.print("You win!, computer chose rock");
                break;
            } else if (isPaper(player1) &amp;&amp; isScissor(random)) {
                System.out.print("Computer chose scissors, computer has won");
                break;
            } else if (isScissor(player1) &amp;&amp; isPaper(random)) {
                System.out.print("You win, computer chose paper");
                break;
            } else if (isRock(player1) &amp;&amp; isScissor(random)) {
                System.out.print("You win!, computer chose scissors");
                break;
            } else if (isScissor(player1) &amp;&amp; isRock(random)) {
                System.out.print("Computer chose rock, computer has won");
                break;
            }
        }
    }

    private static boolean isRock(int answer) {
        return answer == ROCK;
    }

    private static boolean isPaper(int answer) {
        return answer == PAPER;
    }

    private static boolean isScissor(int answer) {
        return answer == SCISSORS;
    }

    private static int getComputerAnswer() {
        int[] conditions = {ROCK, PAPER, SCISSORS};
        return conditions[new Random().nextInt(conditions.length)];
    }

    private static int getPlayer1Answer(Scanner scanner) {
        int value;

        while(true) {
            System.out.print("Please Enter: \n1 for Rock, \n2 for Paper, \n3 for Scissors: ");
            value = scanner.nextInt();

            if(isRock(value) || isPaper(value) || isScissor(value)) {
                break;
            } else {
                System.out.println("Invalid response");
            }
        }

        return value;
    }
}
```