Java方法 - 如何使用方法

时间:2016-10-27 19:21:28

标签: java methods

我正在为我的Java入门课程做作业。我完成了大部分任务:重构我过去创建的Rock,Paper,Scissor游戏,但使用方法。当我的代码执行时;它会打印一条欢迎信息,提示用户输入,然后打印用户的选择和CPU的选择。 我的问题:如何在我的方法中实现while循环,以便在代码执行后它将运行最好的2/3游戏?

据我了解,我知道我必须在每种方法中使用循环。来自用户的输入将不同,并且cpu将生成另一个随机移动。

*注意:我不是要求直接解决方案,而是要求一些概念性的解决方案。

以下是我的代码:

package rpsmethods;

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

public class RPSMethods {

    public static void displayOpeningMessage() {
        System.out.println("Welcome to RPS Game 2.0!");
    }

    public static int getUserMove() {
        Scanner input = new Scanner(System.in);
        System.out.print("Select scissors (0), rock (1), or scissors (2): ");
        int choice = 0;
            if (input.hasNextInt()) {
                choice = input.nextInt();
                switch (choice) {
                    case 0:
                        System.out.println("You chose scissor!");
                        break;
                    case 1:
                        System.out.println("You chose rock!");
                        break;
                    case 2:
                        System.out.println("You chose paper!");
                        break;
                    default:
                        System.out.println("Not a valid input!");
                        System.out.print("Select scissors (0), rock (1), or scissors (2): ");
                        choice = input.nextInt();
                    }
            }
         return choice;
    }

    public static int getCPUMove() {
        Random rnd = new Random();
        int CPUMove = rnd.nextInt(3);
        switch (CPUMove) {
            case 0:
                System.out.println("CPU chose scissor!");
                break;
            case 1:
                System.out.println("CPU chose rock!");
                break;
            case 2:
                System.out.println("CPU chose paper!");
                break;
        }
        return CPUMove;
    }

    public static void determineWinner(int user, int cpu) {
        //User win scenarios
        if (user == 0 && cpu == 2) {
            System.out.println("You win, Scissors beat Paper!");
        } else if (user == 1 && cpu == 0) {
            System.out.println("You win, Rock beats Scissors!");
        } else if (user == 2 && cpu == 1) {
            System.out.println("You win, Paper beats Rock!");
        }
        //Cpu win scenarios
        if (user == 0 && cpu == 1) {
            System.out.println("You lose, Rock beats Scissors!");
        } else if (user == 1 && cpu == 2) {
            System.out.println("You lose, Paper beats Rock!");
        } else if (user == 2 && cpu == 0) {
            System.out.println("You lose, Scissor beats Paper!");
        }
        //Draw scenarios
        if (user == 0 && cpu == 0) {
            System.out.println("You and CPU chose Scissor, it's a draw!");
        } else if (user == 1 && cpu == 1) {
            System.out.println("You and CPU chose Rock, it's a draw!");
        } else if (user == 2 && cpu == 2) {
            System.out.println("You and CPU chose Paper, it's a draw!");
        }
    }

    public static void main(String[] args) {
        displayOpeningMessage();
        int userWinner = getUserMove();
        int CpuWinner = getCPUMove();
        determineWinner(userWinner, CpuWinner);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以定义两个静态变量来检查您的最终赢家  类:

static int cpuScore = 0;  
static int humanScore = 0; 

然后,您可以在determineWinner中为这些变量设置一个值,如下所示:

if (CPU wins) 
  cpuScore++
else 
  humanScore++; 

然后你可以把你的顶级步骤放在这样的循环中:

while (cpuScore < 2 && humanScore < 2) {
  ...
}