Hey guys; writing a game for Pi because it's Pi day and I have absolutely nothing to do. It's most likely made already, probably more than once, but I'm having a bit of trouble; when running the program from here, the console will not wait for input, it just runs and ends saying "Build successful"


Code:

/*
 * Created By Josh Beckwith
 * on March 14, 2012.       
 */
package pigame;

import java.util.Scanner;

public class PiGame
{
    long lPi01 = 1415926535;
    long lPi02 = 897932384;
    long lPi03 = 626433832;
    long lPi04 = 795028841;
    long lPi05 = 971693993;
    long lPi06 = 751058209;
    long lPi07 = 74944592;
    long lPi08 = 30;
    long lPi09 = 781640628;
    long lPi10 = 620899862;
    long lPi11 = 803482534;
    long lPi12 = 211706798;
    static boolean bCorrect = true;
   
    public static void main(String[] args)
    {
        for(int i = 1;bCorrect = false;i++)
        {
            //grab input and store as 'input'
            Scanner input = new Scanner(System.in);
            long iLong;
           
            //take 'input' and set equal to iLong
            //I know i is for integers; but was to lazy to change sorry lol.
            iLong = input.nextLong();
            System.out.print(iLong);
                if(i < 10) {
                   
                } else if (i < 19){
                   
                } else if (i < 28){
                   
                } else if (i < 37){
                   
                } else if (i < 46){
                   
                } else if (i < 55){
                   
                } else if (i < 63){
                   
                } else if (i < 65){
                   
                } else if (i < 74){
                   
                } else if (i < 83){
                   
                } else if (i < 92){
                   
                } else if (i < 101){
                   
                }
        }
    }
}

I believe you should be using haveNextLong to check if there is one to get first, before trying to grab it.
It's because of the "bCorrect = false" in your for-loop (which makes it false and then returns false; so the loop NEVER rus). You probably meant "bCorrect == false", which only tests if it is false (in which case it still never runs, since it is true to begin with). A shorter way to write "bCorrect == false" is just "!bCorrect".
@graphmastur haveNextLong? I've never heard of this before.
@shkaboinka Hmm; tried both ways, "bCorrect == false" and "!bCorrect". Great point out, though the same problem still persists.
joshie75 wrote:
Hmm; tried both ways, "bCorrect == false" and "!bCorrect". Great point out, though the same problem still persists.

That is because you are setting it to true, and wanting to loop while it is false. You either need to set it to false, or loop while it is true, or it will never enter the for-loop.
Oh! @Shka, I thought the second constraint was when you wanted the loop to end. D:

Quick question about the modulus operand.

124 % 100 would produce 1.24 for example, correct?
If so, how do you round that to an even 1?
It is, but "bCorrect = false" always evaluates to false and so the loop doesn't even run once.

Edit in reply to your edit: 124 % 100 would produce 24, not 1. I think you mean 124 / 100, which would produce 1 in any case as both operands are integers and integer/integer yields an integer (rounded towards zero). If you wanted 1.24 you'd need to cast either side to a double or float. % yields the remainder of the division.
I knew int/int produced an integer; but never really messed with the modulus operand.
Assuming from your explanation, wouldn't 124 % 10 be just 4? Or am I making a bad assumption here.

Edit; I'm guessing that's right up there, I didn't see the last line of your post until just now, sorry ^^
124%10 would indeed be 4.
graphmastur wrote:
I believe you should be using haveNextLong to check if there is one to get first, before trying to grab it.

I think it would actually be hasNextLong, not haveNextLong. Wink
Ok guys, I'm pretty sure I'm almost done with the game. This code got a little beefier so if you guys need me to explain anything I did don't hesitate, as you guys are the ones helping me Smile

I ran this code and typed in "1" expecting for it to recognize that my input equaled the first digit to the right of pi.
The console looked like this;
"
Run:
1 //my input
10 //consoles output
Build Successful
"
UPDATE:
Tried it a few more times, and it seems whatever I input the console multiplies it by 10 and spits out that answer. :S

Code:

/*
 * Created By Josh Beckwith
 * on March 14, 2012.       
 */
package pigame;

import java.util.Scanner;

public class PiGame
{
    static int iPi01 = 1415926535;
    static int iPi02 = 897932384;
    static int iPi03 = 626433832;
    static int iPi04 = 795028841;
    static int iPi05 = 971693993;
    static int iPi06 = 751058209;
    static int iPi07 = 74944592;
    static int iPi08 = 30;
    static int iPi09 = 781640628;
    static int iPi10 = 620899862;
    static int iPi11 = 803482534;
    static int iPi12 = 211706798;
    static int iNumber;
    static int iDumber;
    static int iScore = 0;
    static boolean bCorrect = true;
   
    public static void main(String[] args)
    {
        for(int i = 1;bCorrect == true;i++)
        {
            //grab input and store as 'input'
            Scanner input = new Scanner(System.in);
            int iInt;
           
            iInt = input.nextInt();
            System.out.print(iInt);
                if(i < 10) {
                    iNumber = 10-i;
                    iDumber = iPi01/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 19){
                    iNumber = 9-i;
                    iDumber = iPi02/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 28){
                    iNumber = 9-i;
                    iDumber = iPi03/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 37){
                    iNumber = 9-i;
                    iDumber = iPi04/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 46){
                    iNumber = 9-i;
                    iDumber = iPi05/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 55){
                    iNumber = 9-i;
                    iDumber = iPi06/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 63){
                    iNumber = 8-i;
                    iDumber = iPi07/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 65){
                    iNumber = 2-i;
                    iDumber = iPi08/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 74){
                    iNumber = 9-i;
                    iDumber = iPi09/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 83){
                    iNumber = 9-i;
                    iDumber = iPi10/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 92){
                    iNumber = 9-i;
                    iDumber = iPi11/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 101){
                    iNumber = 9-i;
                    iDumber = iPi12/iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                }
        } //ends for loop
    System.out.println(iScore);
    } //ends method
} //ends class
Your code for extracting digits is incorrect. You should be dividing iPi* by 10^iNumber, not just iNumber.
Same problem.
Here's a section of my code, how does it look?

Code:

if(i < 10) {
                    iNumber = 10-i;
                    iDumber = iPi01/10^iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
You should use Math.pow(10,iNumber), the "^" is used in Java for bitwise XOR operations.
Tried doing

Code:

iNumber = Math.pow(10,iNumber);

got an error on the back half of the equation saying it found a double variable but requires an int variable.
Either change the type of iNumber to a double (Perhaps changing the name, too? dNumber?). That, or (And this is probably what you want) cast it to an int by doing:
Code:
iNumber = (int)Math.pow(10, iNumber);
A lot of the things that Math does return doubles, rather than int. You can see what something returns with the online javadoc
Very nice;
My code is semi functional now.

I guess 1; and it moves onto the second digit and I guess 4 and the game ends. The game ends after the second guess no matter the number I chose, I tried 0-9.

Updated;

Code:

/*
 * Created By Josh Beckwith
 * on March 14, 2012.       
 */
package pigame;

import java.util.Scanner;

public class PiGame
{
    static int iPi01 = 1415926535;
    static int iPi02 = 897932384;
    static int iPi03 = 626433832;
    static int iPi04 = 795028841;
    static int iPi05 = 971693993;
    static int iPi06 = 751058209;
    static int iPi07 = 74944592;
    static int iPi08 = 30;
    static int iPi09 = 781640628;
    static int iPi10 = 620899862;
    static int iPi11 = 803482534;
    static int iPi12 = 211706798;
    static int iNumber;
    static int iDumber;
    static int iScore = 0;
    static boolean bCorrect = true;
   
    public static void main(String[] args)
    {
        for(int i = 1;bCorrect == true;i++)
        {
            //grab input and store as 'input'
            Scanner input = new Scanner(System.in);
            int iInt;
           
            iInt = input.nextInt();
            System.out.print(iInt);
                if(i < 10) {
                    iNumber = 10-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi01 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 19){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi02 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 28){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi03 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 37){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi04 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 46){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi05 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 55){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi06 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 63){
                    iNumber = 8-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi07 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 65){
                    iNumber = 2-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi08 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 74){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi09 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 83){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi10 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 92){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi11 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                } else if (i < 101){
                    iNumber = 9-i;
                    iNumber = (int)Math.pow(10, iNumber);
                    iDumber = iPi12 / iNumber;
                        if (iDumber != iInt)
                        {
                            bCorrect = false;
                        }
                        else if (iDumber == iInt)
                        {
                            iScore = iScore + 1;
                        }
                }
        } //ends for loop
    System.out.println(iScore);
    } //ends method
} //ends class

iDumber isn't a single digit number the second time, it's two digits. Use the modulus operator (%) to just get the ones place of iDumber.
I'm curious. Why did you choose to use a bunch of ints, rather than a string? It seems like it would make more sense to do it the latter way, since that's basically what you are trying to hack away at (turning an int into a single digit to see if it matched the number value of the character you input).
I've used strings before, but never tried pulling one character out of a string and comparing it to another. This is my first real java app so I was trying to stick with things I already somewhat knew.

@souvik; very nice catch man. I ran the program as is and it worked if entered 1 for the first digit, 14 for the second, 141 for the third and so on. How can I use the modulus operand to grab the furthest to the right digit? ie the 4 in the second, or the 1 in the third.
Since you already seem to know how to use Scanner, why don't you store the digits in a file and read them in with next() and nextLine()? The constructor for Scanner would look like this:

Code:
Scanner s =new Scanner(new File("C:/file"));


Edit: Just do iDumber%10
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement