【问题标题】:Dice Rolling Program掷骰子程序
【发布时间】:2014-04-21 05:32:11
【问题描述】:

我已经整理了这个程序的一般意义,我只需要“更小或更大”部分的帮助,这使得它无法完成。我知道“”是不可行的;那是一个占位符,直到我知道该放什么。我应该放两行吗?一种用于“=”或其他方法?

问题:

编写一个掷骰子的程序(但对玩家隐藏数字),然后让用户输入一个 1 - 6 范围内的数字。如果玩家输入的计算机掷出的数字相同,那么玩家将获得 10 美元。如果玩家输入的数字小于或大于 1,则玩家将获得 3 美元。如果玩家输入的数字小于或大于 2,则玩家将获得 1 美元。玩家必须支付 3 美元才能玩一次。制作 MatchDice 游戏。提示:使用 Math.random() 方法计算随机值。

我的代码:

import java.util.*;

public class RollDice {

public static void main(String[] args)
{
    Scanner scanner = new Scanner(System.in);
    Random dice = new Random();

    // Generate the random number, between 1 and 6
    int randomValue = (int)(Math.random()*6) + 1;
    int dealerValue = (int)(Math.random()*6) + 1;

    System.out.println("Dealer rolled a dice and here is your turn.");
    System.out.print("Enter a number : ");
    int randomValue = input.nextInt();

    if(randomValue == dealerValue)
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $10 ");
    else if(randomValue == dealerValue <==> 1)
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $3 ")
    else if(randomValue == dealerValue <==> 2)
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $1 );
    else
        System.out.println("You lost $3 ");
}
}

【问题讨论】:

  • 您是在问如何检查一个值是否等于另一个 +/- 某个值?
  • dice 未使用。另外,copypasta 太多了——也许你需要阅读一本关于一般编程的好书
  • 我也闻到这是作业
  • 如果您对解决方案感到满意,请将答案标记为已接受

标签: java dice


【解决方案1】:

使用Math.abs(a - b),其中a 是用户号,b 是……好吧,你知道的。

更新

这是完整的解决方案,已修复错误并将重复次数降至最低。

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    static Random rnd = new Random();

    static int dice() {
        return rnd.nextInt(6) + 1;
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        int dealerValue = dice();

        System.out.print(
            "Dealer rolled a dice and here is your turn.\n" +
            "Enter a number: ");

        int userValue = Integer.parseInt(input.readLine());

        int difference = Math.abs(randomValue - userValue);

        int profit =
            difference == 0 ? 10 :
            difference == 1 ? 3 :
            difference == 2 ? 1 :
            -3;

        System.out.println("Dealer's number was " + dealerValue);
        System.out.println("You " + ((profit >= 0) ? "won" : "lost") + " $" + Math.abs(profit));
    }
}

【讨论】:

    【解决方案2】:

    首先,您需要在条件语句中添加大括号。这段代码甚至无法编译!

    if(randomValue == dealerValue) {
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $10 ");
    }
    else if(Math.abs(randomValue - dealerValue) == 1) {
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $3 ")
    }
    else if(Math.abs(randomValue - dealerValue) == 2) {
        System.out.println("Dealer's number was " + randomValue);
        System.out.println("You won $1 );
    }
    else
        System.out.println("You lost $3 ");
    

    “else”不需要大括号,因为它只包含一个表达式。 编辑: 我忘记了这个问题 - 您可以计算随机值和选定值之间差异的绝对值,并检查它是等于一还是二。

    【讨论】:

    • @SargeBorsch 手机有点分心,我忘了完成问题。谢谢!
    【解决方案3】:

    可能你想写这样的东西:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Generate the random number, between 1 and 6
        int dealerValue = (int)(Math.random()*6) + 1;
    
        System.out.println("Dealer rolled a dice and here is your turn.");
        System.out.print("Enter a number : ");
        int inputValue = scanner.nextInt();
    
        if(inputValue == dealerValue) {
            System.out.println("Dealer's number was " + dealerValue);
            System.out.println("You won $10 ");
        }
        else if(Math.abs(dealerValue - inputValue) == 1) {
            System.out.println("Dealer's number was " + dealerValue);
            System.out.println("You won $3 ");
        }
        else if(Math.abs(dealerValue - inputValue) == 2) {
            System.out.println("Dealer's number was " + dealerValue);
            System.out.println("You won $1 ");
        }
        else {
            System.out.println("Dealer's number was " + dealerValue);
            System.out.println("You lost $3 ");
        }
    
    }
    

    你根本不需要 dice 变量,因为你不使用它。你也不需要用随机数初始化两个变量,一个就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 2019-03-04
      • 2012-02-29
      相关资源
      最近更新 更多