在主方法中调用类

时间:2016-11-22 17:45:26

标签: java class

所以我想创建一个用户可以选择玩两种不同游戏的菜单。我想创建一个main方法,并能够为不同的游戏选项或者用户退出制作for循环或切换语句,但我不确定如何调用这些类以便游戏在选择时运行。

有人可以向我解释我会如何解决这个问题。谢谢!

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

public class RpsGame {

    /* Valid user input: rock, paper, scissors */

    public static void main(String[] args) {

      System.out.print("Please Make Your Choice (Rock, Paper or Scissors): ");

            try {
                Scanner sc = 
                new Scanner(System.in);

                String userInput =   
                sc.next();                      
                if (isValid( userInput )) {
                    game( userInput );

                } else {
                   print("Invalid user input!\nWrite rock, paper or scissors!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void print(String text) {
       System.out.println( text );
    }

    public static boolean isValid(String input) {

        if (input.equalsIgnoreCase("rock")) {
           return true; 
        } 

        if (input.equalsIgnoreCase("paper")) {
           return true; 
        }

         if (input.equalsIgnoreCase("scissors")) {                             
             return true; 
         }

        return false;
    }

    public static void game(String user) {
        String computer = computerResults();
        //System.out.print("Please Make Your Choice: ");

        print( user + " vs " + computer + "\n");

        if (user.equalsIgnoreCase(computer)) {
            print("Oh, Snap! Tied - No winners.");
        } else {

            if (checkWin(user, computer)) {
               print("You won against the computer!");
            } else {
               print("You lost against the computer!");
            }
        }
    }

    public static String computerResults() {

        String types[] = 
        {"rock", "paper", "scissors"};

        Random rand = new Random(); 
        int computerChoice = rand.nextInt(3);;

        return types[computerChoice];
    }

    public static boolean checkWin(String user, String opponent) {

        if ( (!isValid( user )) && (!isValid( opponent )) ) {
            return false;
        }

        String rock = "rock", paper = "paper", scissors = "scissors";

        if ( (user.equalsIgnoreCase( rock )) && (opponent.equalsIgnoreCase( scissors )) ) {
           return true; 
        }

         if ( (user.equalsIgnoreCase( scissors)) && (opponent.equalsIgnoreCase( paper )) ) {
           return true; 
        }

         if ( (user.equalsIgnoreCase( paper )) && (opponent.equalsIgnoreCase( rock  )) ) {
           return true; 
        }

        return false; 
        //If no possible win, assume loss.
    }
}

2 个答案:

答案 0 :(得分:0)

我熟悉的最简单的方法是使用一种称为驱动程序类的方法。 Driver Class是一个用于运行其他类代码的类 - 非常适合运行两个不同的游戏。如果您需要更多信息,请查看此帖:What is a driver class? (Java)

答案 1 :(得分:0)

尝试这样的事情:

public class MyGameApp {
    public static final String OPTION_1 = "1"; 
    public static final String OPTION_2 = "2";
    public static final String OPTION_EXIT = "3";

    public static void main(String... args) {
         Scanner sc = new Scanner(System.in);

         String userChoice = null;
         do {
             System.out.println("Choose an option: \n 1. Game 1\n2. Game 2\n3. Exit");

             userChoice = sc.nextLine();

             switch(userChoice) {
                 case OPTION_1:
                     /*
                       Calls a static method of a class, so there is no need of instantiate the class first. 
                      */
                     GameOne.start();
                     break;
                 case OPTION_2:
                     /*
                      In this case, create a new instance of the class GameTwo, and then call the method start().
                      */
                     GameTwo game = new GameTwo();
                     game.start();
                     break;
                 default:
                     System.out.println("Wrong option, try again.");
             }
         while(!OPTION_EXIT.equals(userChoice));

    }
}

class GameOne {
    public static void start() { ... }
}

class GameTwo {
    public void start() { ... }
}