岩纸剪刀游戏(菜单方法)

时间:2016-04-07 13:50:24

标签: java methods

正如标题中所说,我正在尝试使用菜单作为方法创建RPS游戏,事情是我不知道如何在任何时候从该菜单调用输入。 为了更好的主意,这是我的代码:

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

public class RockPaperScissors
{
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner (System.in);
        String player1choice, player1Name;
        int mainMenu,subMenu;
        String again;
        player1Name = "";


    welcomeBanner ();
    mainMenu = getMenu (keyboard);


    if (mainMenu == 1)
    {
        keyboard.nextLine();
        player1Name = getAName (keyboard);

        for (int i = 0; i < 50; ++i) System.out.println();
        main (null);
    }

    if (mainMenu == 2)
    {
        System.out.println("Welcome "+player1Name); //add name input
        subMenu =getsubMenu (keyboard);
        System.out.println("You have chosen: "); //add option chosen

        System.out.println("Cpu has got, It's a Tie!");//cpuChoice add


    }

    if (mainMenu == 3)
    {
        keyboard.nextLine();
        String exitRequest;
        System.out.print("Are you sure you want to exit? (Y/N): ");
        exitRequest = keyboard.nextLine ();
        if (exitRequest.equals("y") || exitRequest.equals("Y"))
        {
            System.out.println("Good Bye!");
            System.exit(0);
        }
        else if (exitRequest.equals("n") || exitRequest.equals("N"))
        {
            for (int i = 0; i < 50; ++i) System.out.println();
            main(null);
        }

    }
}
static void welcomeBanner()
{
    for (int i = 0; i < 60; i++)
    {
        System.out.print('*');
    }
    System.out.println("");
    System.out.println("*        Welcome To The Rock, Paper, Scissors Game         *");
    System.out.println("*----------------------------------------------------------*");
    System.out.println("*     Created by: Jonathan Gutierrez, and I am NoxBot!     *");
    for (int i = 0; i < 60; i++)
    {
        System.out.print('*');
    }
    System.out.println("");
    System.out.println("");
}
static int getMenu (Scanner aKeyboard)
{
    int playermenuChoice;
    System.out.println("1. Enter Player Name");
    System.out.println("2. Play a Game");
    System.out.println("3. Exit Application");
    System.out.println("");
    System.out.print("Enter your choice: ");
    playermenuChoice = aKeyboard.nextInt();
    return playermenuChoice;

}
static int getsubMenu (Scanner aKeyboard)
{
    int submenuChoice;
    System.out.println("Enter 1 for Rock");
    System.out.println("Enter 2 for Paper");
    System.out.println("Enter 3 for Scissors");
    System.out.println("");
    System.out.print("Enter choice: ");
    submenuChoice = aKeyboard.nextInt();
    return submenuChoice;
}
static String getAName (Scanner aKeyboard)
{
    String player1Info;
    System.out.print("Enter your name: ");
    player1Info = aKeyboard.nextLine ();
    return player1Info;
}
static String computerChoice ()
{
    String cpuChoice;
    cpuChoice = "";
    Random randomNumbers = new Random();
    int cpu = randomNumbers.nextInt (2) + 1;
    switch (cpu)
    {
        case 1:
        cpuChoice = "Rock";
        break;
        case 2:
        cpuChoice = "Paper";
        break;
        case 3:
        cpuChoice = "Scissors";
        break;
    }
    return cpuChoice;
}

因此,当玩家选择选项1时,程序会要求输入播放器的名称,我想在任何时候使用该输入(最明确的是当mainMenu == 2时)。我怎么能这样做?

编辑:这是我的新代码:

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner (System.in);
        String player1choice, player1Name, subMenu;
        int mainMenu;
        String again;
        player1Name = "";

        welcomeBanner ();
        mainMenu = getMenu (keyboard);

    if (mainMenu == 1)
    {
        keyboard.nextLine();
        player1Name = getAName (keyboard);

        for (int i = 0; i < 50; ++i) System.out.println();
        welcomeBanner ();
        mainMenu = getMenu (keyboard);
        System.out.println("");
        System.out.println("Welcome " + player1Name);
        System.out.println("");

    }

    if (mainMenu == 2)
    {

        subMenu =enterPlayersChoice (keyboard);
        keyboard.nextLine();
        String cmpu = computerChoice ();
        for(int i = 0; i < 3; i ++)

        if (subMenu.equals(cmpu))
           System.out.println("It's a tie!");
        else if (subMenu.equals("rock"))
        if (cmpu.equals("scissors"))
        System.out.println("Rock crushes scissors. You win!!");
        else if (cmpu.equals("paper"))
        System.out.println("Paper eats rock. You lose!!");
        else if (subMenu.equals("paper"))
        if (cmpu.equals("scissors"))
        System.out.println("Scissor cuts paper. You lose!!");
        else if (cmpu.equals("rock"))
        System.out.println("Paper eats rock. You win!!");
        else if (subMenu.equals("scissors"))
        if (cmpu.equals("paper"))
        System.out.println("Scissor cuts paper. You win!!");
        else if (cmpu.equals("rock"))
        System.out.println("Rock breaks scissors. You lose!!");
        else   System.out.println("Invalid user input.");
        System.out.println("");


    }

    if (mainMenu == 3)
    {
        keyboard.nextLine();
        String exitRequest;
        System.out.print("Are you sure you want to exit? (Y/N): ");
        exitRequest = keyboard.nextLine ();
        if (exitRequest.equals("y") || exitRequest.equals("Y"))
        {
            System.out.println("Good Bye!");
            System.exit(0);
        }
        else if (exitRequest.equals("n") || exitRequest.equals("N"))
        {
            for (int i = 0; i < 50; ++i) System.out.println();
            main(null);
        }

    }
}
static void welcomeBanner()
{
    for (int i = 0; i < 60; i++)
    {
        System.out.print('*');
    }
    System.out.println("");
    System.out.println("*        Welcome To The Rock, Paper, Scissors Game         *");
    System.out.println("*----------------------------------------------------------*");
    System.out.println("*     Created by: Jonathan Gutierrez, and I am NoxBot!     *");
    for (int i = 0; i < 60; i++)
    {
        System.out.print('*');
    }
    System.out.println("");
    System.out.println("");
}
static int getMenu (Scanner aKeyboard)
{
    int playermenuChoice;
    System.out.println("1. Enter Player Name");
    System.out.println("2. Play a Game");
    System.out.println("3. Exit Application");
    System.out.println("");
    System.out.print("Enter your choice: ");
    playermenuChoice = aKeyboard.nextInt();
    return playermenuChoice;

}
public static String enterPlayersChoice(Scanner aKeyboard)
{
    String input = "";
    System.out.print("You have a choice of picking rock, paper, or scissors: ");
    input = aKeyboard.nextLine();
    String inputLower = input.toLowerCase();
    return inputLower;
 }
static String getAName (Scanner aKeyboard)
{
    String player1Info;
    System.out.print("Enter your name: ");
    player1Info = aKeyboard.nextLine ();
    return player1Info;
}
public static String computerChoice ()
{
    String cpuChoice;
    cpuChoice = "nothing";
    Random randomNumbers = new Random();
    int cpu = randomNumbers.nextInt (2) + 1;
    switch (cpu)
    {
        case 1:
        cpuChoice = "rock";
        break;
        case 2:
        cpuChoice = "paper";
        break;
        case 3:
        cpuChoice = "scissors";
        break;
    }
    return cpuChoice;
}

} 为了完成这个我想让游戏显示一个消息,无论玩家输赢,但是它被跳过(mainMenu == 2)任何想法?

1 个答案:

答案 0 :(得分:2)

这是一种以不同方式重新排列现有应用程序的方法。一些主要的变化包括使所有方法都是非静态的,除了main方法,并为应用程序的入口点创建一个RockPaperScissorsNew对象。我还添加了类变量,因此您无需将扫描程序作为对象传递给所有方法。

为了回答您如何重新使用用户输入的输入的原始问题,我提供的解决方案是将该信息保留在类变量中。

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


public class RockPaperScissorsNew {

    //Class variables
    Scanner keyboard;
    String player1choice, player1Name; //Name will be stored here.
    int mainMenu,subMenu;

    public RockPaperScissorsNew()
    {
        keyboard = new Scanner(System.in);  
        welcomeBanner();  //Display the welcome banner once.
        while(true)       //Repeatedly display the main menu.
            getChoice();  //Get the user's choice
    }

    public void getChoice()
    {
        int choice = -1;          //Set choice to fail first.
        while (choice > 3 || choice < 0)  //Wait until user choice passes.
        {
            choice = getMenu();  
        }
        if (choice == 1) { //Choose your sub option.
            getAName();    // Get the user name.
            System.out.println("Your name is " + player1Name); //Saved
        }
        if (choice == 2)
            getsubMenu();
        if (choice == 3)
            System.exit(0);
    }

    public void welcomeBanner()
    {
        for (int i = 0; i < 60; i++)
        {
            System.out.print('*');
        }
        System.out.println("");
        System.out.println("*        Welcome To The Rock, Paper, Scissors Game         *");
        System.out.println("*----------------------------------------------------------*");
        System.out.println("*     Created by: Jonathan Gutierrez, and I am NoxBot!     *");
        for (int i = 0; i < 60; i++)
        {
            System.out.print('*');
        }
        System.out.println("");
        System.out.println("");
    }

    public int getMenu ()
    {
        int playermenuChoice;
        System.out.println("1. Enter Player Name");
        System.out.println("2. Play a Game");
        System.out.println("3. Exit Application");
        System.out.println("");
        System.out.print("Enter your choice: ");
        playermenuChoice = Integer.parseInt(keyboard.nextLine().trim());
        return playermenuChoice;
    }

    public int getsubMenu ()
    {
        int submenuChoice;
        System.out.println("Enter 1 for Rock");
        System.out.println("Enter 2 for Paper");
        System.out.println("Enter 3 for Scissors");
        System.out.println("");
        System.out.print("Enter choice: ");
        submenuChoice = Integer.parseInt(keyboard.nextLine().trim());
        return submenuChoice;
    }

    //This method has been changed to use the class variable, and no longer
    //returns a string.
    public void getAName ()
    {
        //String player1Info;
        System.out.print("Enter your name: ");
        player1Name = keyboard.nextLine ();
        //return player1Info;
    }

    public String computerChoice ()
    {
        String cpuChoice;
        cpuChoice = "";
        Random randomNumbers = new Random();
        int cpu = randomNumbers.nextInt (2) + 1;
        switch (cpu)
        {
            case 1:
            cpuChoice = "Rock";
            break;
            case 2:
            cpuChoice = "Paper";
            break;
            case 3:
            cpuChoice = "Scissors";
            break;
        }
        return cpuChoice;
    }

    public static void main(String...args)
    {
        new RockPaperScissorsNew();
    }
}