输入验证出现问题并再次播放会循环JAVA作业

时间:2018-10-10 06:09:50

标签: java loops variable-assignment

我为CS1类分配的任务是创建一个类似于Rock,Paper和Scissors的游戏。输入是1、2、3、4和5。我需要帮助弄清楚如何提示用户输入正确的输入,以防他们输入的数字不是1-5。而且我还需要插入“再次播放?”的帮助。环。请帮助我。我真的很挣扎。

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 :(得分:0)

首先考虑为您的消息创建一个数组

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]);
}

当然,您需要将所有消息添加到数组中,并且您可能想进一步测试无效输入,但是至少此方法可以防止很多重复的代码

答案 1 :(得分:0)

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;
    }

}
}