gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:27:14 25 4
gpt4 key购买 nike

我正在为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com