可能的范围问题(也许)?

时间:2016-05-16 06:53:32

标签: java while-loop scope

所以我正在制作一个小命令行彩票游戏(代码中的注释解释了所有内容)但是当我生成随机数时,我的代码并没有像我希望的那样将它缩短为三位数,除了游戏的工作原理并且可玩每隔一段时间,一个随机生成的数字超过一千,不会缩短。任何想法(对不起,如果它很难读,我不知道很多java概念,所以我一直坚持使用基本的逻辑运算符)

/* Dustin Shropshire 5.14.2016 

test program : LotteryGame ,
generate a random number and then separate them into their place values
and store them in variables allows user to take a guess at the value
allows user to take a guess at the value and then take their input
and determine if all the numbers match in the exact order, or if they
match in any order, or if at least one of the numbers match */




import java.util.Scanner;

public class LotteryGame {
public static void main(String[] args){

    Scanner input = new Scanner(System.in);

        int  lotteryDigitNumber1 = 0, lotteryDigitNumber2 = 0, lotteryDigitNumber3 = 0; // the lottery numbers  place to hold its three digits 
        int userGuessInput = 0, userGuessDigit1 = 0, userGuessDigit2 = 0, userGuessDigit3 = 0;  // users guess and a place to hold its three digits
        int seperateLotteryNumber = 0, seperateUserNumber = 0; // used to seperate the number 
        int shortenVariable = 0; //used to shorten a randomaly generated variable if its over 1000;
        int moneyCounter = 10000; //starting amount of money
        int randomLotteryNumber = 0; //initialzing this out of the loop in the hopes that it puts everythin in scope


    while(moneyCounter > 0){

         randomLotteryNumber = (int)(Math.random() * 1000 + 100 );  // generates a random number between 0 and 1100 exlusing 1100

        System.out.println("please enter a three digit number as your guess:");

        userGuessInput = input.nextInt();

     //this while loop is used to prevent someone from entering a number less than 100 and greater than 999
    while((userGuessInput >= 1000) || (userGuessInput <= 99)){ 

        System.out.println("sorry that is not three digit number please enter a three digit number:");

        userGuessInput = input.nextInt(); } 

        System.out.println("the random lotttery number was " + " " + randomLotteryNumber);

    // this if statment is used if a randomly generated number is over 1000 to truncate to a 3 digit number
    if(randomLotteryNumber >= 1000){  

            shortenVariable = randomLotteryNumber / 10; 

        lotteryDigitNumber1 = shortenVariable / 100; 
        seperateLotteryNumber = shortenVariable % 100; 
        lotteryDigitNumber2 = seperateLotteryNumber / 10; 
        lotteryDigitNumber3 = seperateLotteryNumber % 10; 
    }

    else if( randomLotteryNumber <= 999){  

        lotteryDigitNumber1 = randomLotteryNumber / 100;
        seperateLotteryNumber = randomLotteryNumber % 100;
        lotteryDigitNumber2 = seperateLotteryNumber / 10;
        lotteryDigitNumber3 = seperateLotteryNumber % 10;}
        //used to seperate lottery numbers digits into individual variables


        userGuessDigit1 = userGuessInput / 100;
        seperateUserNumber = userGuessInput % 100;
        userGuessDigit2 = seperateUserNumber /10;
        userGuessDigit3 = seperateUserNumber % 10;
        //used to seperate lottery number digits into individual variables 


    if(userGuessInput == randomLotteryNumber ) { 

            moneyCounter = moneyCounter + 10000;

            System.out.println("the lottery number was:" + " " + randomLotteryNumber + " "  + "you guessed right you earn $10,000" 
            + " " + "currentmoney:" + " " + moneyCounter);

        //this if statment is only true when both the lottery number and the user guess is exactly matched
        } 
    else if ((userGuessDigit1 == lotteryDigitNumber1 && userGuessDigit2 == lotteryDigitNumber3 && userGuessDigit3 == lotteryDigitNumber2
            || userGuessDigit1 == lotteryDigitNumber2 && userGuessDigit2 == lotteryDigitNumber1 && userGuessDigit3 == lotteryDigitNumber3
            || userGuessDigit1 == lotteryDigitNumber2 && userGuessDigit2 == lotteryDigitNumber3 && userGuessDigit3 == lotteryDigitNumber1
            || userGuessDigit1 == lotteryDigitNumber3 && userGuessDigit2 == lotteryDigitNumber2 && userGuessDigit3 == lotteryDigitNumber1 
            || userGuessDigit1 == lotteryDigitNumber3 && userGuessDigit2 == lotteryDigitNumber1 && userGuessDigit3 == lotteryDigitNumber2)){
                //this logic block is true when the lottery number and user guess have all the same numbers but in a different order 

                moneyCounter = moneyCounter + 3000;

                System.out.println("the lottery number was:" + " " + randomLotteryNumber + " "  + "you guessed all the numbers right just in the wrong order you earn $3,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);



            }
    else if ( (userGuessDigit1 == lotteryDigitNumber1) || (userGuessDigit1 == lotteryDigitNumber2) || (userGuessDigit1 == lotteryDigitNumber3)
                || (userGuessDigit2 == lotteryDigitNumber1) || (userGuessDigit2 == lotteryDigitNumber2) || (userGuessDigit2 == lotteryDigitNumber3)
                || (userGuessDigit3 == lotteryDigitNumber1) || (userGuessDigit3 == lotteryDigitNumber2) || (userGuessDigit3 == lotteryDigitNumber3)){
                                //this logic block is only true when the user guesses one of the same numbers as the lottery number 

                                moneyCounter = moneyCounter + 1000;

                                System.out.println("the lottery number was:" + " " + randomLotteryNumber + " "  + "you guessed one number right you earn $1,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);

                            }
    else if( (userGuessDigit1 != lotteryDigitNumber1 && userGuessDigit2 != lotteryDigitNumber3 && userGuessDigit3 != lotteryDigitNumber2
            || userGuessDigit1 != lotteryDigitNumber2 && userGuessDigit2 != lotteryDigitNumber1 && userGuessDigit3 != lotteryDigitNumber3
            || userGuessDigit1 != lotteryDigitNumber2 && userGuessDigit2 != lotteryDigitNumber3 && userGuessDigit3 != lotteryDigitNumber1
            || userGuessDigit1 != lotteryDigitNumber3 && userGuessDigit2 != lotteryDigitNumber2 && userGuessDigit3 != lotteryDigitNumber1 
            || userGuessDigit1 != lotteryDigitNumber3 && userGuessDigit2 != lotteryDigitNumber1 && userGuessDigit3 != lotteryDigitNumber2)){
                             //this logic block is used if none of the numbers in any order is matched 

                            moneyCounter = moneyCounter - 1000; 

            System.out.println("oh sadly none of the numbers match you lose  $1000"  +  " "  + "current money:"  +  " " +  moneyCounter);
            }




}


    System.out.println("your out of money thanks for playing");
        }

    }

4 个答案:

答案 0 :(得分:0)

这不是一个范围问题,这是一个错误地使用Math.random()的问题。 Math random返回0(包括)和1(不包括)之间的值,并且您似乎隐含地假设它将具有不超过一位精度。 Math.random()可能会返回0.913之类的数字,其乘以1000将为913,然后当您添加100时,1013将成为Random rand = new Random(); // should be done only once when the program starts int randomLotteryNumber = 100 + rand.nextInt(900); ,给你一个四位数字。生成三位数整数的正确方法是执行以下操作:

 <script>
     var alpha = ["this", "is", "array"],
     beta = ["this"];

    $.arrayIntersect = function(a, b)
    {
        return $.grep(a, function(i)
        {
            return $.inArray(i, b) > -1;
        });
    };

    console.log( $.arrayIntersect(alpha, beta) );
 </script>

答案 1 :(得分:0)

Math.Random()会返回0.00.99...

的数字

将它与1000相乘可能会给你一个999这样的数字,+ 100给出一个四位数字。

这是一个有点复杂的解决方案

randomLotteryNumber = (randomLotteryNumber = (int)(Math.random() * 1000)) > 99 ? 
                                              randomLotteryNumber : 
                                              randomLotteryNumber + 100;

另一种简单的方法是使用Java Streams

Random r = new Random();
randomLotteryNumber = r.ints(100, 1000).findFirst().getAsInt();

这将返回从100(包括)到1000(不包括)的随机整数。

答案 2 :(得分:0)

我相信你的问题在于你的打印输出。您正在打印randomLotteryNumber而不是从生成的数字中选择的3位数字。看到你永远不会修改那个数字,你总是打印出你生成的数字。

您的代码中的一个示例:

System.out.println("the lottery number was:" + " " + randomLotteryNumber + " "  + "you guessed one number right you earn $1,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);

你应该打印(toString()部分只是为了确保没有进行任何算术运算,可能会用其他的替换):

System.out.println("the lottery number was:" + " " + lotteryDigitNumber1.toString()+lotteryDigitNumber2.toString()+lotteryDigitNumber3.toString()+ " "  + "you guessed one number right you earn $1,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);

答案 3 :(得分:0)

得到了一个答案我打印错误的值,如果一个大于一千的数字中有0,则会出现逻辑问题,

import java.util.Scanner;

public class LotteryGame {
public static void main(String[] args){

    Scanner input = new Scanner(System.in);

        int  lotteryDigitNumber1 = 0, lotteryDigitNumber2 = 0, lotteryDigitNumber3 = 0; // the lottery numbers  place to hold its three digits 
        int userGuessInput = 0, userGuessDigit1 = 0, userGuessDigit2 = 0, userGuessDigit3 = 0;  // users guess and a place to hold its three digits
        int seperateLotteryNumber = 0, seperateUserNumber = 0; // used to seperate the number 
        int shortenVariable = 0; //used to shorten a randomaly generated variable if its over 1000;
        int moneyCounter = 10000; //starting amount of money
        int randomLotteryNumber = 0; //initialzing this out of the loop in the hopes that it puts everythin in scope


    while(moneyCounter > 0){

         randomLotteryNumber = (int)(Math.random() * 1000 + 100 );  // generates a random number between 0 and 1100 exlusing 1100

        System.out.println("please enter a three digit number as your guess:");

        userGuessInput = input.nextInt();

     //this while loop is used to prevent someone from entering a number less than 100 and greater than 999
    while((userGuessInput >= 1000) || (userGuessInput <= 99)){ 

        System.out.println("sorry that is not three digit number please enter a three digit number:");

        userGuessInput = input.nextInt(); } 



    // this if statment is used if a randomly generated number is over 1000 to truncate to a 3 digit number
    if(randomLotteryNumber >= 1000){  

            shortenVariable = randomLotteryNumber / 10; 

        lotteryDigitNumber1 = shortenVariable / 100; 
        seperateLotteryNumber = shortenVariable % 100; 
        lotteryDigitNumber2 = seperateLotteryNumber / 10; 
        lotteryDigitNumber3 = seperateLotteryNumber % 10; 

    //may delete this logic later
    if(lotteryDigitNumber2 == 0){
        lotteryDigitNumber3 = seperateLotteryNumber;
    }


    }

    else if( randomLotteryNumber <= 999){  

        lotteryDigitNumber1 = randomLotteryNumber / 100;
        seperateLotteryNumber = randomLotteryNumber % 100;
        lotteryDigitNumber2 = seperateLotteryNumber / 10;
        lotteryDigitNumber3 = seperateLotteryNumber % 10;
        shortenVariable = randomLotteryNumber;
        }
        //used to seperate lottery numbers digits into individual variables

        System.out.println("the random number is" + " " + shortenVariable);

        userGuessDigit1 = userGuessInput / 100;
        seperateUserNumber = userGuessInput % 100;
        userGuessDigit2 = seperateUserNumber /10;
        userGuessDigit3 = seperateUserNumber % 10;
        //used to seperate lottery number digits into individual variables 


    if(userGuessInput == shortenVariable ) { 

            moneyCounter = moneyCounter + 10000;

            System.out.println("the lottery number was:" + " " + shortenVariable + " "  + "you guessed right you earn $10,000" 
            + " " + "currentmoney:" + " " + moneyCounter);

        //this if statment is only true when both the lottery number and the user guess is exactly matched
        } 
    else if ((userGuessDigit1 == lotteryDigitNumber1 && userGuessDigit2 == lotteryDigitNumber3 && userGuessDigit3 == lotteryDigitNumber2
            || userGuessDigit1 == lotteryDigitNumber2 && userGuessDigit2 == lotteryDigitNumber1 && userGuessDigit3 == lotteryDigitNumber3
            || userGuessDigit1 == lotteryDigitNumber2 && userGuessDigit2 == lotteryDigitNumber3 && userGuessDigit3 == lotteryDigitNumber1
            || userGuessDigit1 == lotteryDigitNumber3 && userGuessDigit2 == lotteryDigitNumber2 && userGuessDigit3 == lotteryDigitNumber1 
            || userGuessDigit1 == lotteryDigitNumber3 && userGuessDigit2 == lotteryDigitNumber1 && userGuessDigit3 == lotteryDigitNumber2)){
                //this logic block is true when the lottery number and user guess have all the same numbers but in a different order 

                moneyCounter = moneyCounter + 3000;

                System.out.println("the lottery number was:" + " " + shortenVariable + " "  + "you guessed all the numbers right just in the wrong order you earn $3,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);



            }
    else if ( (userGuessDigit1 == lotteryDigitNumber1) || (userGuessDigit1 == lotteryDigitNumber2) || (userGuessDigit1 == lotteryDigitNumber3)
                || (userGuessDigit2 == lotteryDigitNumber1) || (userGuessDigit2 == lotteryDigitNumber2) || (userGuessDigit2 == lotteryDigitNumber3)
                || (userGuessDigit3 == lotteryDigitNumber1) || (userGuessDigit3 == lotteryDigitNumber2) || (userGuessDigit3 == lotteryDigitNumber3)){
                                //this logic block is only true when the user guesses one of the same numbers as the lottery number 

                                moneyCounter = moneyCounter + 1000;

                                System.out.println("the lottery number was:" + " " + shortenVariable + " "  + "you dident guess all the numbers but got some right you earn $1,000" 
                                    + " " + "currentmoney:" + " " + moneyCounter);

                            }
    else if( (userGuessDigit1 != lotteryDigitNumber1 && userGuessDigit2 != lotteryDigitNumber3 && userGuessDigit3 != lotteryDigitNumber2
            || userGuessDigit1 != lotteryDigitNumber2 && userGuessDigit2 != lotteryDigitNumber1 && userGuessDigit3 != lotteryDigitNumber3
            || userGuessDigit1 != lotteryDigitNumber2 && userGuessDigit2 != lotteryDigitNumber3 && userGuessDigit3 != lotteryDigitNumber1
            || userGuessDigit1 != lotteryDigitNumber3 && userGuessDigit2 != lotteryDigitNumber2 && userGuessDigit3 != lotteryDigitNumber1 
            || userGuessDigit1 != lotteryDigitNumber3 && userGuessDigit2 != lotteryDigitNumber1 && userGuessDigit3 != lotteryDigitNumber2)){
                             //this logic block is used if none of the numbers in any order is matched 

                            moneyCounter = moneyCounter - 1000; 

            System.out.println("oh sadly none of the numbers match you lose  $1000"  +  " "  + "current money:"  +  " " +  moneyCounter);
            }




}


    System.out.println("your out of money thanks for playing");
        }

    }