Sorry, I'm very new to programming and I'm trying to make a rock, paper, scissors program. It seems to work fine, but once I put in the input, (rock, paper, scissors), it gives me my result and then terminates in the console. How can I get it so I can play more rounds and it saves my score without having to run the program again. I tried using for and while loops but all it does is print the result of the game as many times as it loops and then terminates. Again sorry if this has been asked or is super obvious I've just been looking everywhere and have no idea. MAIN CODE
public class RockPaperScissorsMain {
public static void main(String[] args) {
System.out.println("Hello and welcome to Rock, Paper, Scissors");
System.out.println("Please select either Rock, Paper or Scissors...");
System.out.println("You can type \"Quit\" at any time to exit the game =)");
ActionClass myMethod= new ActionClass();
myMethod.method();
}
}
CODE
package rockPaperScissors;
import java.util.Scanner;
import java.util.Random;
public class ActionClass {
int winCounter=0;
int loseCounter=0; //COUNTER VARIABLES
int tieCounter=0;
String[] selection= {"Rock", "Paper", "Scissors"};
Random rand =new Random(); // VARIABLES
Scanner scanny = new Scanner(System.in);
char inputChar= scanny.nextLine().charAt(0);
String opponentSelection= selection[rand.nextInt(3)];
char playerSelection= Character.toLowerCase(inputChar);
public void method() {
if(playerSelection=='q') {
System.out.println("===============================================================================");
System.out.println("THANK YOU FOR PLAYING TAKE CARE!!!");
System.out.println("==============================================================================");
System.exit(0); }
else if (playerSelection=='r') {
System.out.println("you said "+selection[0]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " + opponentSelection);
System.out.println("======================================================================================================================================================");
}
else if(playerSelection== 'p') System.out.println("you said " +selection[1]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else if( playerSelection=='s') System.out.println("you said " +selection[2]+ "\t\t\t\t\t\t\t\t\t\t\t Your opponent chooses... " +opponentSelection);
else System.out.println("\nSorry please select a valid option of ROCK, PAPER or SCISSORS");
if (opponentSelection.equals(selection[0])) { System.out.println("you TIED"); tieCounter++;}
if (opponentSelection.equals(selection[1])) {System.out.println("you LOSE"); loseCounter++;}
if (opponentSelection.equals(selection[2])) { System.out.println("you WIN"); winCounter++;}
if ((inputChar=='r')||inputChar=='p'||inputChar=='s') {System.out.println("Good Game!!!");
System.out.println("\n\n\t\t\t\t\tPlay again!! please select either Rock, Paper or Scissors...");
System.out.println("\n\t\t\t\t\tYou've WON: "+winCounter+ " times!!!" +"You\'ve TIED " +tieCounter+" times!!! You've LOST: " +loseCounter+ " times!!!" );
}
}}
Aucun commentaire:
Enregistrer un commentaire