Проблема с проверкой входных данных и воспроизведением повторяет домашнюю работу JAVA - PullRequest
0 голосов
/ 10 октября 2018

Мое задание для моего класса CS1 - создать игру, похожую на Rock, Paper, Scissors.Это 1, 2, 3, 4 и 5. Мне нужна помощь, чтобы выяснить, как предложить пользователю ввести правильные данные в случае, если они вводят число, которое не 1-5.И мне также нужна помощь, чтобы вставить «Играть снова?»Loop.Пожалуйста, помогите, я так застрял.Я действительно борюсь.

import java.util.Scanner;
import java.util.Random;

public class Moropinzee {
    public static void main(String[] args) {
        String personPlay1;
        String personPlay2;
        String response;

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("Hey, Let's Play Moropinzee!\n" + "Please enter a move.\n"
                + "Monkey = 1, Robot = 2, Pirate = 3, Ninja= 4, and Zombies = 5.");

        System.out.println("Player 1 plays: ");
        personPlay1 = scan.next();

        System.out.println("Player 2 plays ");
        personPlay2 = scan.next();

        if (personPlay1.equals(personPlay2))
            System.out.println("It's a tie!");

        else if (personPlay1.equals("1"))
            if (personPlay2.equals("2"))
                System.out.println("Monkey unplugs Robot. Player 1 win!!");
        if (personPlay1.equals("2"))
            if (personPlay2.equals("1"))
                System.out.println("Monkey unplugs Robot. Player 2 win!!");

        if (personPlay1.equals("1"))
            if (personPlay2.equals("3"))
                System.out.println("Pirate skewers Monkey. Player 2 win!!");
        if (personPlay1.equals("3"))
            if (personPlay2.equals("1"))
                System.out.println("Pirate skewers Monkey. Player 1 win!!");

        if (personPlay1.equals("1"))
            if (personPlay2.equals("4"))
                System.out.println("Monkey fools Ninja. Player 1 win!!");
        if (personPlay1.equals("4"))
            if (personPlay2.equals("1"))
                System.out.println("Monkey fools Ninja. Player 2 win!!");

        if (personPlay1.equals("1"))
            if (personPlay2.equals("5"))
                System.out.println("Zombie savages Monkey. Player 2 win!!");
        if (personPlay1.equals("5"))
            if (personPlay2.equals("1"))
                System.out.println("Zombie savages Monkey. Player 1 win!!");

        if (personPlay1.equals("2"))
            if (personPlay2.equals("3"))
                System.out.println("Pirate drowns Robot. Player 2 win!!");
        if (personPlay1.equals("3"))
            if (personPlay2.equals("2"))
                System.out.println("Pirate drowns Robot. Player 1 win!!");

        if (personPlay1.equals("2"))
            if (personPlay2.equals("4"))
                System.out.println("Robot chokes Ninja. Player 1 win!!");
        if (personPlay1.equals("4"))
            if (personPlay2.equals("2"))
                System.out.println("Robot chokes Ninja. Player 2 win!!");

        if (personPlay1.equals("2"))
            if (personPlay1.equals("5"))
                System.out.println("Robot crushes Zombie. Player 1 win!!");
        if (personPlay1.equals("5"))
            if (personPlay1.equals("2"))
                System.out.println("Robot crushes Zombie. Player 2 win!!");

        if (personPlay1.equals("3"))
            if (personPlay2.equals("4"))
                System.out.println("Ninja karate chops Pirate. Player 2 win!!");
        if (personPlay1.equals("4"))
            if (personPlay2.equals("3"))
                System.out.println("Ninja karate chops Pirate. Player 1 win!!");

        if (personPlay1.equals("3"))
            if (personPlay2.equals("5"))
                System.out.println("Zombie eats Pirate. Player 2 win!!");
        if (personPlay1.equals("5"))
            if (personPlay2.equals("3"))
                System.out.println("Zombie eats Pirate. Player 1 win!!");

        if (personPlay1.equals("4"))
            if (personPlay2.equals("5"))
                System.out.println("Ninja decapitates Zombie. Player 1 win!!");
        if (personPlay1.equals("5"))
            if (personPlay2.equals("4"))
                System.out.println("Ninja decapitates Zombie. Player 2 win!!");

            else {

            }

    }
}

Ответы [ 2 ]

0 голосов
/ 10 октября 2018

Код, предоставленный Scary Wombat, находится в большинстве нормально, однако вы должны использовать два цикла while (вместо одного), чтобы избежать повторения программы с самого начала.И не забывайте разрывать обе петли после того, как они закончили свои задачи.

import java.util.Scanner;

public class Moropinzee {
public static void main(String[] args) {
    int Player1;
    int Player2;

    String [][] results = new String [6][6];
    results[1][2] = "Monkey unplugs Robot. Player 1 win!!";
    results[2][1] = "Monkey unplugs Robot. Player 2 win!!";
    results[1][3] = "Pirate skewers Monkey. Player 2 win!!";
    results[3][1] = "Pirate skewers Monkey. Player 1 win!!";
    results[1][4] = "Monkey fools Ninja. Player 1 win!!";
    results[4][1] = "Monkey fools Ninja. Player 2 win!!";
    results[1][5] = "Zombie savages Monkey. Player 2 win!!";
    results[5][1] = "Zombie savages Monkey. Player 1 win!!";
    results[2][3] = "Pirate drowns Robot. Player 2 win!!";
    results[3][2] = "Pirate drowns Robot. Player 1 win!!";
    results[2][4] = "Robot chokes Ninja. Player 1 win!!";
    results[4][2] = "Robot chokes Ninja. Player 2 win!!";
    results[2][5] = "Robot crushes Zombie. Player 1 win!!";
    results[5][2] = "Robot crushes Zombie. Player 2 win!!";
    results[3][4] = "Ninja karate chops Pirate. Player 2 win!!";
    results[4][3] = "Ninja karate chops Pirate. Player 1 win!!";
    results[3][5] = "Zombie eats Pirate. Player 2 win!!";
    results[5][3] = "Zombie eats Pirate. Player 1 win!!";
    results[4][5] = "Ninja decapitates Zombie. Player 1 win!!";
    results[5][4] = "Ninja decapitates Zombie. Player 2 win!!";

    Scanner scan = new Scanner(System.in);

    System.out.println("Hey, Let's Play Moropinzee!\n" + "Please enter a move.\n"
            + "Monkey = 1, Robot = 2, Pirate = 3, Ninja= 4, and Zombies = 5.");



    while (true) {      
        System.out.println("Player 1 plays: ");
        Player1 = scan.nextInt();
        if (Player1 < 1 || Player1 > 5) {System.out.println("Please, enter a number in a range 1-5!!!");
                continue;}

        while (true) {
            System.out.println("Player 2 plays ");
            Player2 = scan.nextInt();
            if (Player2 < 1 || Player2 > 5) {System.out.println("Please, enter a number in a range 1-5!!!");
                continue;}

            if (Player1==Player2) System.out.println("It's a tie!");
            else System.out.println(results[Player1][Player2]);
            System.out.println();
        break;
        } 

        System.out.println("Enter Yes (Y or y) if you'd like to continue, or any other letter to quit");
        String choise = scan.next();
        if (choise.equals("Y") || choise.equals("y")) continue;
        else System.out.println("The game is over.");
            break;
    }

}
}
0 голосов
/ 10 октября 2018

Прежде всего, подумайте о создании массива для ваших сообщений

String [][] msgs = new String [6][6];
msgs[1][2] = "Monkey unplugs Robot. Player 1 win!!";
msgs[2][1] = "Monkey unplugs Robot. Player 2 win!!";
// etc

Scanner scan = new Scanner(System.in); 
// get input
while (true) {
    System.out.println("enter number 1 or -1 to quit");
    int num1 = scan.nextInt();
    if (num1 == -1) break;
    if (num1 < 1 || num1> 5) {
        continue;
    }
    System.out.println("enter number 2");
    int num2 = scan.nextInt();
    if (num2 < 1 || num2> 5) {
        continue;
    }

    System.out.println(msgs[num1][num2]);
}

Конечно, вам нужно добавить все сообщения в массив, и вы, возможно, захотите провести дальнейшее тестирование некорректного ввода, но, по крайней мере, этометод предотвращения большого дублирования кода

...