jeudi 28 septembre 2017

How can I make it so if the first player inputs a wrong command then it will a specific prompt?

I am trying to make a game of rock-paper-scissors-spock-lizard. I have been learning about if/else statements. I am trying to make it so that if player1 inputs an invalid command then it will print out "Sorry, this is not a valid command" like with the else statement. It works if both players input a command and one is wrong, but not if just player1 did. Could you help me figure out what I am supposed to do? Here is my code:

    package csc212hw03;
    import java.util.Scanner;

    public class Main {

public static void main(String[] args) {
    String player1;
    String player2;
    String player1Choice;
    String player2Choice;
    String line;

           // “1” for Paper
    //“2” for Rock
    //“3” for Spock
    //“4” for Lizard
    //“5”for Scissors
   Scanner kb = new Scanner(System.in);
   System.out.println("Player 1, please enter your name:");
   player1 = kb.nextLine();

   System.out.println("Player 2, please enter your name:");
   player2 = kb.nextLine();

   System.out.println(player1 + ", please enter your command:");
   player1Choice = kb.nextLine();

   System.out.println(player2 + ", please enter your command:");
   player2Choice = kb.nextLine();

   if (player1Choice.equals("1") && player2Choice.equals("2")) {
       System.out.println(player1 + " wins! Paper covers Rock.");
       System.out.println("Thank you for playing.");
   } else if (player2Choice.equals("2") && player1Choice.equals("1")){
       System.out.println(player2 + " wins! Paper covers Rock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("1")&& player2Choice.equals("1")) {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("2")&& player2Choice.equals("2")) {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("1")&& player2Choice.equals("3")) {
       System.out.println(player1 + " wins! Paper disproves Spock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("3") && player2Choice.equals("1")) {
       System.out.println(player2 + " wins! Paper disproves Spock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("3")&& player2Choice.equals("3"))  {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("2") && player2Choice.equals("3")) {
       System.out.println(player2 + " wins! Spock vaporizes Rock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("3") && player2Choice.equals("2")) {
       System.out.println(player1 + " wins! Spock vaporizes Rock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("3") && player2Choice.equals("5")) {
       System.out.println(player1 + " wins! Spock smashes Scissors.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("3")) {
       System.out.println(player2 + " wins! Spock smashes Scissors.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("5")) {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("2") && player2Choice.equals("5")) {
       System.out.println(player1 + " wins! Rock crushes Scissors.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("2")) {
       System.out.println(player2 + " wins! Rock crushes Scissors.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("2") && player2Choice.equals("4")) {
       System.out.println(player1 + " wins! Rock crushes Lizard.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("4") && player2Choice.equals("2")) {
       System.out.println(player2 + " wins! Rock crushes Lizard.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("3") && player2Choice.equals("4")) {
       System.out.println(player2 + " wins! Lizard poisons Spock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("4") && player2Choice.equals("3")) {
       System.out.println(player1 + " wins! Lizard poisons Spock.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("1")) {
       System.out.println(player1 + " wins! Scissors cuts Paper.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("1") && player2Choice.equals("5")) {
       System.out.println(player2 + " wins! Scissors cuts Paper.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("4")) {
       System.out.println(player1 + " wins! Scissors decpitates Lizard.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("4") && player2Choice.equals("5")) {
       System.out.println(player2 + " wins! Scissors decapitates Lizard.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("5") && player2Choice.equals("5")) {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("4") && player2Choice.equals("4")) {
       System.out.println("Draw!");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("4") && player2Choice.equals("1")) {
       System.out.println(player1 + " wins! Lizard eats Paper.");
       System.out.println("Thank you for playing.");
   } else if (player1Choice.equals("1") && player2Choice.equals("4")) {
       System.out.println(player2 + " wins! Lizard eats Paper.");
       System.out.println("Thank you for playing.");
   } else {
       System.out.println("I'm sorry, this is not a valid command.");
       System.out.println("Thank you for playing.");

   }
      }
    }

Aucun commentaire:

Enregistrer un commentaire