有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Main方法中调用类

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

有人能告诉我怎么做吗。谢谢

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) 个答案

  1. # 1 楼答案

    我所熟悉的最简单的方法是使用一种叫做Driver类的东西。Driver类是设计用来运行其他类的代码的类,非常适合运行两个不同的游戏。如果需要更多信息,请查看此帖子:What is a driver class? (Java)

  2. # 2 楼答案

    试试这样:

    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() { ... }
    }