【问题标题】:Java Dice game issueJava骰子游戏问题
【发布时间】:2014-12-06 18:50:43
【问题描述】:

任务是让用户输入一笔钱作为赌注(这个数字必须大于0赌注>=1)。

1) 掷出两个骰子 (1-6)。

2) 计算它们的总和。如果这个总和等于 7,则用户赢 4 美元,如果输了,用户输 1 美元(例如:用户输入 5 美元作为初始赌注,赢 = 5+4=9$ 现在 - 输 = 5-1 =现在 4 美元)。

在用户玩完之后,我们需要询问他们是否想再玩一次,本质上是使用 while 语句再次运行程序。

我在第 43 行和第 50 行注释掉了要打印的语句,因为会发生这两行将在用户想要输入多少的初始问题之前打印(这就是我注释掉它们的原因)。

现在的问题是,在用户输入他/她想要下注的金额后,系统会计算他们赢/输的金额并打印出来。但是,它会跳过询问他们是否想再次播放的打印语句。为什么是这样?我知道出了点问题,因为最初程序跳过了第一个打印语句,直接进入第二个和第三个函数。有什么帮助吗?

谢谢!

//编辑:另外,如果还不是很明显,我对编程很陌生,所以请随时以任何方式批评我^^

  //Dice Game
//10/11/14
import java.util.Random;
import java.util.Scanner;

public class diceGame2
{
    //Instance Variables
    private String play = "yes";
    private int cash;
    private int sum;
    private int winnings;
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();


    //Constructor
    public diceGame2()
    {
        cash=0;
        sum=0;
        winnings=0;
    }
    public void game()
    {
    do
    {
        System.out.println("Please enter the amount of money you would like to bet");
        cash=scan.nextInt();
        if(cash<1)
        {
            System.out.println("Please enter a bet greater than 1$.");
            cash=scan.nextInt();
        }
    }while((play.equals("yes")&&cash<0));
    }
    public int getDice()
    {

        int dice = rand.nextInt(6) + 1;
        System.out.println("The number rolled was a" + " " + dice);
        System.out.println();
        return dice;
    }
    public int getSum(int d1, int d2)
    {
        sum = d1+d2;
        System.out.println("Their sum is" + " " + sum + " " + "so...");
        System.out.println();
        return sum;
    }
    public int getWinnings(int sum)
    {
    if(sum==7)
    {
        cash += (4);
        System.out.println("You have won" + " " + "4$.");
    }
    else
    {
        cash -= (1);
        System.out.println("You have lost" + " " + "1$.");
    }
    System.out.println("You current have" + " " + cash  + " " + "dollars left to bet");
    return winnings;    
    }
    public String getPlay()
    {
        System.out.println("Do you want to play again");
        play = scan.next();
        return play;
    }

}//Class




//Dice Game Runner
//10/11/14
public class diceGameRunner
{
    public static void main(String[]args)
    {
    String play = "yes";
    while((play.equals("yes")))
    {
    diceGame2 d1 = new diceGame2();
    int dice1, dice2, sum;
    double money;

    d1.game();
    dice1 = d1.getDice();
    dice2 = d1.getDice();
    sum = d1.getSum(dice1, dice2);
    money = d1.getWinnings(sum);
    play = d1.getPlay();
    }


    }

}

【问题讨论】:

  • 我只是略过这个,所以我还没有正确阅读它(还),但是你马上就有了一个带有无条件break 语句的while 循环,所以循环只会执行一次。不知道这是否是您的问题,但它看起来像一个错误。换句话说,while 的作用等同于 if。
  • 您需要发布您的主要方法或显示您的方法被调用的顺序
  • @bcsb1001 我放 break 语句的唯一原因是因为没有它,它会卡在 while 语句中。我知道由于 break 语句它只会运行一次,我只是不确定在询问 print 语句后如何让它离开循环。
  • 尝试将play.equals("yes"))||(play.equals("Yes")替换为play.equalsIgnoreCase("yes")&amp;&amp;cash&lt;1,并去掉if和break。
  • 这就是为什么教师让你在编码之前编写流程图。

标签: java while-loop dice


【解决方案1】:

您的代码存在一些问题。 第一:你说过没有 break 语句,你的 while 循环永远不会中断。这是因为条件永远为真。你永远不会改变循环中的条件,总是强制它为真。

第二:你有“cash =- (-1);”那里有一个额外的 - 登录。

最后(更多的旁注):您的对象中有循环。这将使您难以调用任何方法并编写清晰的代码。尝试将 while 循环移到对象之外,并使对象的方法只执行一项任务。

编辑:这是您应该做什么的示例代码。它在大约 10 分钟内完成,应该修改为您想要的。这是一个关于如何设置类和执行循环的示例。

主要方法:

import java.util.Scanner;

/**
 *
 * @author Tyler Weaver
 */
public class Test {
    public static void main(String[] args) {
        String play = "yes";
        int cash;
        Scanner scan = new Scanner(System.in);

        do {
            System.out.printf("Enter cash amount: ");
            cash = scan.nextInt();
        } while (cash < 1);

        DiceGame game = new DiceGame(cash);

        while (play.equalsIgnoreCase("yes") && cash > 0) {
            int dice1 = game.roll();
            int dice2 = game.roll();

            game.calcCash(dice1 + dice2);
            cash = game.getCash();
            System.out.printf("Your new cash amount is $%,.2f%n", (float) cash);
            System.out.printf("Do you want to play again? ");

            play = scan.next();
        }
    }
}

这是一个减去其他必要方法的类应该是什么样子的示例:

import java.util.Random;

/**
 *
 * @author Tyler Weaver
 */
public class DiceGame {
    private int cash;
    private Random gen;

    public DiceGame(final int cash) {
        this.cash = cash;
        gen = new Random();
    }

    public int roll() {
        return gen.nextInt(6) + 1;
    }

    public void calcCash(final int sum) {
        cash += (sum == 7) ? 4 : -1;
    }

    public int getCash() {
        return cash;
    }
}

这不是答案。这是您的问题中的示例代码。您将必须弄清楚如何实现您所需要的。

请注意该类如何仅对每个方法执行一项操作。这只是编码约定。还要注意我是如何在 main 方法的循环外索要现金并在内部计算的。我还问他们是否想在方法结束时再次播放以更新条件“播放”。现金通过类中的方法更新。

【讨论】:

  • 上传后,我修复了break语句和cash = - (-1)。我现在有我的 while:while((play.equalsIgnoreCase("yes")&&cash
  • 我不会直接给你答案,试着让你对象的所有方法都做一个任务。如果你需要写更多的方法,那就去做吧。但是一个对象的方法应该只产生一个输出。编写该代码后,您可以在 main 方法中包含任何条件。例如,在 main 方法中有一个 do while 循环来玩游戏,而答案是肯定的。您可以在循环结束时请求“是”。
  • 在没有看到你先尝试的情况下,我很犹豫要不要写代码。
  • 我想我明白了。我将我的while循环放在我的主循环中,并将循环外的字符串播放初始化为“是”。所以现在游戏结束后出现提示,如果是==是/是,它允许用户输入并重新运行程序。感谢您的帮助!
  • 我遇到的另一个问题是如何将值 cash 设置为系统将再次使用的一个值。换言之,用户第一次输入值x。然后他们输了。所以 x-1 = 现金。如果用户想再玩一次,他们以 x-1 的值作为 cash:: 开始,或者如果他们赢了,x+4 作为他们的现金值,当这个值达到 0 或用户想要时,系统将结束停止。关于如何做到这一点的任何提示?我尝试更改 while 语句,以便当值大于 1 时,它会跳过该提示,但到目前为止没有任何工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多