I enjoyed the many headaches this project gave me as I learned a lot! I have been studying Java for less than a year and am new to programming in general. I read over the comments rules and hope I followed them correctly. I don't know many people that code and would love any feedback. This was a school project I worked very hard on, but I know I have a long way to go. Please, let me know if you can help me get better...
import java.util.Scanner;
import java.util.Random;
public class RPS {
public static void main(String[] args) {
Boolean playAgain = true;
Scanner input = new Scanner(System.in);
String play;
while (playAgain = true) {
WelcomeP1(); // Welcome & basic rule example
// Final Result & Replay Option
System.out.print("*********************" );
System.out.print("-------------> You " +Game()+ "!!!" );
System.out.println("\n*********************" );
System.out.println("Play Again?!");
System.out.println("Please press y for (Y)es or n for (N)o: ");
play = input.nextLine().trim();
while (play.isEmpty()){
System.out.println("Do you want to play Rock, Paper, Scissors?");
System.out.println("Please press y for (Y)es \nor n for (N)o: ");
play = input
.nextLine().trim();
} // end while ~ validate/catch error ~ for empty space
switch ( play.charAt(0) ){
case 'n' : case 'N':
playAgain = false;
System.exit(0);
case 'y': case 'Y':
System.out.println("\n*********************" );
System.out.print("Excellent;\nGlad you enjoyed playing!");
System.out.println("\nJust checking again...");
} // end while
} // end switch
} // end main
// Welcome Validation Start
public static void WelcomeP1(){
Scanner stdIn= new Scanner(System.in);
Boolean validGame = false;
String userPlay;
while( validGame == false ){
System.out.println("Do you want to play Rock, Paper, Scissors?");
System.out.println("Please press y for (Y)es \nor n for (N)o: ");
userPlay = stdIn.nextLine().trim();
while (userPlay.isEmpty()){
System.out.println("Do you want to play Rock, Paper, Scissors?");
System.out.println("Please press y for (Y)es \nor n for (N)o: ");
userPlay = stdIn.nextLine().trim();
} // end while ~ validate/catch error ~ for empty space
// below valiates user entry
switch ( userPlay.charAt(0) ){
case 'n' : case 'N':
System.exit(0);
case 'y': case 'Y':
validGame = true;
System.out.println("*************************************");
System.out.println("Excellent! Thank you...\nLet's Play!");
break;
default:
} // end switch
} // end while loop
} // end class Welcome ~ End Welcome Validation
// User Input Validation Start
public static int P1Choice(){
Scanner userIn = new Scanner(System.in);
Boolean validUserIn = false;
String userInput;
int userPiece = 0;
while (validUserIn == false){
System.out.println("*************************************");
System.out.println("Choose (R)ock, (P)aper, or (S)cissors ");
System.out.println("Please type the first letter only.");
System.out.println("Your Choice: ");
userInput = userIn.nextLine();
while (userInput.isEmpty()){
System.out.println("Choose (R)ock, (P)aper, or (S)cissors ");
System.out.println("Please type the first letter only.");
System.out.println("Your Choice: ");
userInput = userIn.nextLine();
} // end while ~ validate/catch error ~ for empty space
// below valiates user entry further
userPiece = userInput.charAt(0);
if (userInput.equalsIgnoreCase("r")){
userPiece = 0;
System.out.println("You Chose: Rock");
validUserIn = true;
}
else if (userInput.equalsIgnoreCase("p")){
userPiece = 1;
System.out.println("You Chose: Paper");
validUserIn = true;
}
else if (userInput.equalsIgnoreCase("s")) {
userPiece = 2;
System.out.println("You Chose: Scissors");
validUserIn = true;
}
else {
System.out.println("Invalid Choice.");
validUserIn = false;
}
} // end while loop
return userPiece;
} // end class UserPiece ~ End User Input Validation & Return
// Computer Choice Calculation & Print ~ Start
public static int ComputerChoice(){
Random random = new Random();
int choiceStart;
choiceStart = random.nextInt(3); // max number 3 ~ 0-2
int computerInput = 0;
String computerPiece = null;
switch (choiceStart) {
case 0:
computerInput = 0;
computerPiece = "Rock";
break;
case 1:
computerInput = 1; //Paper
computerPiece = "Paper";
break;
case 2:
computerInput = 2; //Scissors
computerPiece = "Scissors";
break;
} // end switch
System.out.println("*************************************");
System.out.println("*************************************");
System.out.println("The computer has decided as well...");
System.out.println("|||||||||||||||||||||||||||||||||||||");
System.out.println("|||||||||||||||||||||||||||||||||||||");
System.out.println("The computer's choice was " +computerPiece+ "...");
System.out.println("*************************************");
System.out.println("*************************************");
return computerInput;
} // end class ComputerChoice ~ End Computer Choice Calculation & Print
// Calculate Game & Return Win/Loss/Tie ~ Calculations
public static String Game(){
int user = P1Choice();
int computer = ComputerChoice();
String error = "*** If you are reading this, there has been a"
+" system error. Please restart the program. ***";
// 0 = rock
// 1 = paper
// 2 = scissors
if (user == 0) {
if (computer != 1){
if (computer != 0){
return "Win";
}
return "Tie";
}
return "Lose";
} // end first nested for loop logic
if (user == 1){
if (computer != 2){
if (computer != 1) {
return "Win";
}
return "Tie";
}
return "Lose";
} // end second nested for loop logic
if (user == 2){
if (computer != 0){
if (computer != 2) {
return "Win";
}
return "Tie";
}
return "Lose";
} // end third nested for loop logic
return error; // null -- Needed to return something
// -- better practice teacher?
} // end class Game ~ End Calculate Game & Return
} // end class RPS
```