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

时间:2016-02-09 19:32:10

标签: java variables methods scope java.util.scanner

我正在为Java编写一个Craps骰子程序。我无法弄清楚如何将主方法中的Scanner“playerName”传递给playgame方法和rolldice方法。我希望能够让程序在这两种方法中打印播放器名称。 (示例“Jake滚动4和5总共9”而不是“玩家滚动4和5总共9”。我也得到警告Resouce Leak:'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;
    }
}

2 个答案:

答案 0 :(得分:1)

从用户获取名称后,您必须将playerName作为String参数传递给您要使用它的任何函数。 我还把你的班级名改为骆驼案。

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;
    }
}

答案 1 :(得分:0)

为了将参数传递给方法,需要在方法声明

中声明它们
public void playGame(String playerName) {
   ...
}

然后按以下方式调用方法

String playerName = sc.nextLine();
playGame(playerName);

就您收到的警告而言,您需要在使用它时关闭扫描仪,即

sc.close();

我希望有帮助并欢迎编程!