创建彩票

时间:2015-02-09 22:19:59

标签: java user-input

生成三位数的抽奖。程序提示用户输入一个三位数字和 根据以下规则确定用户是否获胜: (规则1)。如果用户输入按照确切的顺序与彩票号码匹配,则奖励为 $ 10,000。 (规则2)。如果用户输入中的所有数字都与彩票号码中的所有数字相匹配,则奖励为 $ 3,000。 (规则3)。如果用户输入中的一个数字与彩票号码中的数字匹配,则奖励为 $ 1,000。

我需要帮助让我的程序按照预期在代码中看到的所有功能运行。没有数组,没有字符串,除了那里已有的东西。我的问题是我无法猜测110忽略规则2.我将其设置为100以测试所有规则。

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args) {
        int lottery = 100;

 // Prompt the user to enter a guess
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();

 // Get digits from lottery
        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = (lottery % 100) / 10;
        int lotteryDigit3 = lottery % 10;

 // Get digits from guess
        int guessDigit1 = guess / 100;
        int guessDigit2 = (guess % 100) / 10;
        int guessDigit3 = guess % 10;

        System.out.println("The lottery number is " + lottery);

 // RULE1 Check the guess
        if (guess == lottery)
            System.out.println("Exact match: you win $10,000");
 // RULE2   
        else if ((guessDigit1 == lotteryDigit1
            || guessDigit1 == lotteryDigit2 
            || guessDigit1 == lotteryDigit3)
            && (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit2
            || guessDigit2 == lotteryDigit3)
            && (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2
            || guessDigit3 == lotteryDigit3))
            System.out.println("Match all digits: you win $3,000");
    // RULE3
        else if ((guessDigit1 == lotteryDigit1
            || guessDigit1 == lotteryDigit2
            || guessDigit1 == lotteryDigit3)
            || (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit2
            || guessDigit2 == lotteryDigit3)
            || (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2
            || guessDigit3 == lotteryDigit3))   
            System.out.println("Match one digit: you win $1,000");

        else
            System.out.println("Sorry, no match");
    }
 }

更新:

import java.util.Scanner;
    public class NewClass {
        public static void main(String[] args) {
        int lottery = 456;

 // Prompt the user to enter a guess
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();

 // Get digits from lottery
        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = (lottery % 100) / 10;
        int lotteryDigit3 = lottery % 10;

 // Get digits from guess
        int guessDigit1 = guess / 100;
        int guessDigit2 = (guess % 100) / 10;
        int guessDigit3 = guess % 10;

        System.out.println("The lottery number is " + lottery);

  // Sum up both sets of digits to compare for 3 inconsecutive matches
        int guessSum = guessDigit1 + guessDigit2 + guessDigit3;
        int lotterySum = lotteryDigit1 + lotteryDigit2 + lotteryDigit3;

 // RULE1 Check the guess
        if (guess == lottery)
            System.out.println("Exact match: you win $10,000");
 // RULE2   
        else if ((guessDigit1 == lotteryDigit1
            || guessDigit1 == lotteryDigit2 
            || guessDigit1 == lotteryDigit3)
            && (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit2
            || guessDigit2 == lotteryDigit3)
            && (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2
            || guessDigit3 == lotteryDigit3)
            && guessSum == lotterySum)      
            System.out.println("Match all digits: you win $3,000");
// RULE3
        else if ((guessDigit1 == lotteryDigit1
            || guessDigit1 == lotteryDigit2
            || guessDigit1 == lotteryDigit3)
            || (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit2
            || guessDigit2 == lotteryDigit3)
            || (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2
            || guessDigit3 == lotteryDigit3))   
            System.out.println("Match one digit: you win $1,000");

        else
            System.out.println("Sorry, no match");
    }
 }

这似乎最重要。我一直骑着数字来测试猜测。我没有遇到错误的猜测。 && guessSum == lotterySum)仅用于RULE2。

7 个答案:

答案 0 :(得分:0)

这里的问题是你有重复,所以规则2的当前条件评估为真为100< => 110

为了消除这种影响,通常的变体是对数字和数字进行排序,比如这样(伪语言):

if (sortDigits(lottery) == sortDigits(guess))

如果你不能使用数组,这里有简单的函数来排序3位数字:

int sortDigits(int n) {
int d1 = n/100;
int d2 = (n/10)%10;
int d3 = n%10;

int min = Math.min(d1, Math.min(d2, d3));
int max = Math.max(d1, Math.max(d2, d3));
int mid = -1;
if (min == d1) mid = Math.min(d2, d3);
else if (min == d2) mid = Math.min(d1, d3);
else mid = Math.min(d1, d2);

return (min*100) + (mid*10) + max;
}

答案 1 :(得分:0)

你的问题是你用抽奖的每个数字检查猜测的每个数字。这就是您收到错误的原因,您需要记住您所匹配的内容。 为了做到这一点,一个非常快速和肮脏的解决方案是检查双精度并用一个永远不会匹配的值替换它们,如-1:

    if(guessDigit1 == guessDigit2){
        guessDigit1 = -1;
    }
    else if (guessDigit1 == guessDigit3 ) {
        guessDigit1 = -1;
    }
    else if (guessDigit2 == guessDigit3 ) {
        guessDigit2 = -1;
    }

请注意,这根本不是最佳解决方案!

更新:为了记住您检查的内容,您必须实施许多条件。其中一个的伪代码如下:

If guessDigit2 == lotteryDigit1 
    if guessDigit1 == lotteryDigit2 
        if guessDigit3 == lotteryDigit3
             -> You found all digits
    else if guessDigit1 == lotteryDigit3
        if guessDigit3 == lotteryDigit2
        -> You found all digits

当然你必须实现这个代码* 3,因为你需要为guessDigit1 == lotteryDigit1和guessDigit3 == lotteryDigit1创建另外2个ifs。

答案 2 :(得分:0)

你对规则2的条件是错误的;它目前正在说(伪代码):

IF  guessDigit1 is anyLotteryDigit
AND guessDigit2 is anyLotteryDigit
AND guessDigit3 is anyLotteryDigit

这显然不是您想要的,因为它意味着多个猜测数字可以匹配相同的彩票数字,因为您遇到110匹配100 - 从{{ 1}}和1都在中奖号码中,即使它不应该通过。

相反,您希望将每个数字与剩余的数字相匹配。执行此操作的“正确”方法是Set,但听起来你还不能使用它们。你可以手动完成,写出所有案例只需要做一些工作。基本上,您将猜测的一位数与抽奖的一位数进行比较,然后将剩余的两个猜测数字与剩余的两个抽奖数字进行比较。每个数字冲洗重复。

答案 3 :(得分:0)

正如@ dimo414(以及其他答案)所提到的,您需要某种内存来检查您要检查的数字。以下条件将为您提供您正在寻找的排列。如果最后matched1matched2matched3-1,则表示不匹配。

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args) {
        int lottery = 100;

        // Prompt the user to enter a guess
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();

        // Get digits from lottery
        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = (lottery % 100) / 10;
        int lotteryDigit3 = lottery % 10;

        // Get digits from guess
        int guessDigit1 = guess / 100;
        int guessDigit2 = (guess % 100) / 10;
        int guessDigit3 = guess % 10;

        System.out.println("The lottery number is " + lottery);

        int matched1 = -1;
        int matched2 = -1;
        int matched3 = -1;

        // RULE1 Check the guess
        if (guess == lottery) {
            System.out.println("Exact match: you win $10,000");
        } 
        // RULE2   
        else {
            if (guessDigit1 == lotteryDigit1) {
                matched1 = 1;
                if (guessDigit2 == lotteryDigit2) {
                    matched2 = 2;
                    if (guessDigit3 == lotteryDigit3) {
                        matched3 = 3;
                    }
                } else if (guessDigit2 == lotteryDigit3) {
                    matched2 = 3;
                    if (guessDigit3 == lotteryDigit2) {
                        matched3 = 2;
                    }
                }
            } else if (guessDigit1 == lotteryDigit2) {
                matched1 = 2;
                if (guessDigit2 == lotteryDigit1) {
                    matched2 = 1;
                    if (guessDigit3 == lotteryDigit3) {
                        matched3 = 3;
                    }
                } else if (guessDigit2 == lotteryDigit3) {
                    matched2 = 3;
                    if (guessDigit3 == lotteryDigit1) {
                        matched3 = 1;
                    }
                }
            } else if (guessDigit1 == lotteryDigit3) {
                matched1 = 3;
                if (guessDigit2 == lotteryDigit1) {
                    matched2 = 1;
                    if (guessDigit3 == lotteryDigit2) {
                        matched3 = 2;
                    }
                } else if (guessDigit2 == lotteryDigit2) {
                    matched2 = 2;
                    if (guessDigit3 == lotteryDigit1) {
                        matched3 = 1;
                    }
                }
            }

            if (matched1 != -1 && matched2 != -1 && matched3 != -1) {
                System.out.println("Match all digits: you win $3,000");
            } 
            // RULE3
            else if ((guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3)
                    || (guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3)
                    || (guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3)) {  
                    System.out.println("Match one digit: you win $1,000");
            } else {
                System.out.println("Sorry, no match");
            }
        }
    }
 }

首先检查猜测的第一个数字是否是彩票的第一个数字。如果是这样,它将匹配1(对于您的猜测数字)保存为值1(对于抽奖中的第一个数字)。然后它检查第二个猜测的数字与彩票的数字2和3相比较。我们不会在这里再次检查您的猜测的第二位数,因为我们已经知道第一次猜测与第一次抽奖相匹配。如果第一个数字与第一个彩票号码不匹配,那么我们看看猜测的第一个数字是否是彩票号码的第二个数字,然后冲洗并重复。 (如果是这种情况,那么匹配1对于抽奖的第二位数将是2。)

(匹配1,匹配2,匹配3)的所有匹配值为(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3, 1,2)和(3,2,1)。您可以在上面的嵌套if语句中看到所有这些排列都包含在内。

答案 4 :(得分:0)

我想我知道这一点,也许是因为规则2中的110是真的,而规则3也是如此,这就是为什么它显示两个规则的输出。这是我对rule2的布尔表达式

((guessDigit1 == lotteryDigit2
            && guessDigit2 == lotteryDigit3
            && guessDigit3 == lotteryDigit1)
            || (guessDigit1 == lotteryDigit3
            && guessDigit2 == lotteryDigit1
            && guessDigit3 == lotteryDigit2)
            || (guessDigit1 == lotteryDigit1
            && guessDigit2 == lotteryDigit3
            && guessDigit3 == lotteryDigit2)
            || (guessDigit1 == lotteryDigit3
            && guessDigit2 == lotteryDigit2
            && guessDigit3 == lotteryDigit1)
            || (guessDigit1 == lotteryDigit2
            && guessDigit2 == lotteryDigit1
            && guessDigit3 == lotteryDigit3))

所有可能的答案都在那里。

答案 5 :(得分:-1)

我认为问题在于你没有考虑重复数字。您可以检查以确保所有数字与样本编号相加,即:

1+1+0 = 2
1+0+0 = 1
1!=2
Move onto rule 3. 

使用此代码:

guessDigit1  + guessDigit2 + guessDigit3 == lotteryDigit1 + lotteryDigit2 + lotteryDigit3

我不知道这是否会解决您的所有问题,但这是一个开始,它应该用'110'值解决您的直接问题。祝你好运!

答案 6 :(得分:-2)

相反,您希望将每个数字与剩余数字相匹配。执行此操作的“正确”方法是使用Set,但听起来您还不能使用它们。你可以手动完成,写出所有案例只需要做一些工作。基本上,您将猜测的一个数字与抽奖的一个数字进行比较,然后将剩余的两个猜测数字与剩余的两个抽奖数字进行比较。每个数字冲洗重复。

会是这样的:

else if ((guessDigit1 == lotteryDigit2 
            || guessDigit1 == lotteryDigit3)
            && (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit3)
            && (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2))
            System.out.println("Match all digits: you win $3,000");

这是我可以使用的全部 - 没有其他公式。用于输入代码的程序对输入非常特别。它只会接受它已接受的内容。