骰子滚动计划

时间:2014-03-15 21:47:35

标签: java dice

我已经把这个程序的一般意义放在一起,我只需要帮助#34;更小或更大的"部分,这是保持它完成。我知道"< ==>"不可行;那是作为占位符,直到我知道放在那里的东西。我应该放两条线吗?一个用于"< ="和#34;> ="还是其他一些方法?

问题:

编写一个掷骰子的程序(但隐藏播放器中的数字),然后让用户输入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 ");
}
}

3 个答案:

答案 0 :(得分:3)

使用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));
    }
}

答案 1 :(得分:1)

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

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”不需要大括号,因为它只包含一个表达式。 编辑: 我忘记了这个问题 - 你可以计算随机值和选定值之间差值的绝对值,并检查它是否等于一个或两个。

答案 2 :(得分:0)

可能你想写这样的东西:

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

}

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