java - 将输入从扫描仪传递到方法

标签 java variables methods scope java.util.scanner

我正在为 Java 编写一个掷骰子程序。我不知道如何将扫描仪“playerName”从 main 方法传递到 playgame 方法和 rolldice 方法中。我希望能够让程序用这两种方法打印玩家姓名。 (例如“Jake 掷出 4 和 5,总共 9 点”,而不是“玩家掷出 4 和 5,总共 9 点”。我还收到警告资源泄漏:“sc”从未关闭。我是新手编程场景,所以如果你能用简单的术语解释我将不胜感激。也欢迎任何改进程序的建议。

import java.util.Scanner;
import java.util.Random;
public class crapsgame
{
    //generates random number to be used in method rollDice
    private Random randomNumbers = new Random();

    //enumeration of constants that represent game status
    private enum Status {WIN, LOSE, CONTINUE};

    //represents possible outcomes of rolling the dice
    private final static int two = 2;
    private final static int three = 3;
    private final static int seven = 7;
    private final static int eleven = 11;
    private final static int twelve = 12;

    public static void main (String[]args)
    {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a player name: ");
        String playerName = sc.nextLine();
        crapsgame game = new crapsgame(); //created object of class "crapsgame"
        game.playgame(); //tells crapsgame object "game" to invoke"playgame" method
    }

    //method to play game
    public void playgame()
    {
        int currentPoint = 0; //holds point value for current roll
        Status gameResult; //contains one of enumeration values
        int sumofDice = rollDice(); //sum after first roll
        //determines if won, lost, or continue
        switch (sumofDice)
        {
            case seven:
            case eleven:
                gameResult = Status.WIN;
                break;
            case two:
            case three:
            case twelve:
                gameResult = Status.LOSE;
                break;
            //game continues if above conditions are not met
            default:
                gameResult = Status.CONTINUE;
                currentPoint = sumofDice;
                System.out.printf("Point is %d\n", currentPoint);
        }

        while (gameResult == Status.CONTINUE)
        {
            sumofDice = rollDice();
            if (sumofDice == currentPoint)
                gameResult = Status.WIN;
            else
                if (sumofDice == seven)
                    gameResult = Status.LOSE;
        }
        if (gameResult == Status.WIN)
            System.out.println("Player wins");
        else 
            System.out.println ("Player loses");
    }

    public int rollDice()
    {
        //choose a random number from 1-6
        int firstroll = 1 + randomNumbers.nextInt(6);
        int secondroll = 1 + randomNumbers.nextInt(6);
        int sum = firstroll + secondroll;
        System.out.printf("Player rolled %d and %d for a total of %d\n", firstroll, secondroll, sum);
        return sum;
    }
}

最佳答案

从用户那里获取名称后,您必须将playerName作为字符串参数传递给您想要使用它的任何函数。 我还将你的类名更改为驼峰命名法。

import java.util.Scanner;
import java.util.Random;
public class CrapsGame {
    //generates random number to be used in method rollDice
    private Random randomNumbers = new Random();

    //enumeration of constants that represent game status
    private enum Status {WIN, LOSE, CONTINUE};

    //represents possible outcomes of rolling the dice
    private final static int two = 2;
    private final static int three = 3;
    private final static int seven = 7;
    private final static int eleven = 11;
    private final static int twelve = 12;

    public static void main (String[]args) {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter a player name: ");
        String playerName = sc.nextLine();
        CrapsGame game = new CrapsGame(); //created object of class "CrapsGame"
        game.playgame(playerName); //tells CrapsGame object "game" to invoke"playgame" method
    }
    //method to play game
    public void playgame(String playerName) {
        int currentPoint = 0; //holds point value for current roll
        Status gameResult; //contains one of enumeration values
        int sumofDice = rollDice(playerName); //sum after first roll
        //determines if won, lost, or continue
        switch (sumofDice)
        {
            case seven:
            case eleven:
            gameResult = Status.WIN;
            break;
            case two:
            case three:
            case twelve:
            gameResult = Status.LOSE;
            break;
            //game continues if above conditions are not met
            default:
            gameResult = Status.CONTINUE;
            currentPoint = sumofDice;
            System.out.printf("Point is %d\n", currentPoint);
        }

        while (gameResult == Status.CONTINUE)
        {
            sumofDice = rollDice(playerName);
            if (sumofDice == currentPoint)
                gameResult = Status.WIN;
            else if (sumofDice == seven)
                gameResult = Status.LOSE;
        }
        if (gameResult == Status.WIN)
            System.out.println(playerName + " wins");
        else 
            System.out.println (playerName + " loses");
    }
    public int rollDice(String playerName)
    {
        //choose a random number from 1-6
        int firstroll = 1 + randomNumbers.nextInt(6);
        int secondroll = 1 + randomNumbers.nextInt(6);
        int sum = firstroll + secondroll;
        System.out.printf("%s rolled %d and %d for a total of %d\n", playerName, firstroll, secondroll, sum);
        return sum;
    }
}

关于java - 将输入从扫描仪传递到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300646/

相关文章:

java - 如何将对象的Arraylist转换为Javascript?

Java - 如何在 switch 语句中调用方法?

c - 当只需要少量时,使用 unsigned char 类型是否可以接受?

C#类默认方法

javascript - 元素在过渡时重叠的 setInterval 函数,

java - 查找至少有 n 个元素与另一个键相同的键,包含列表

java - 使用 Java 的 Android 填字游戏算法

php - MYSQL 使用变量更新多列

javascript - Internet Explorer 9 无法获取属性错误的值

java - 我在调用 Java 中的静态递归方法时遇到问题