0

Ok so here it is: I ask for the numbers picked
Then ask for the numbers that are drawn
Then when I goto so if they match up its telling me that pickOne can not be resolved but I'm using the users' input, what I have I done wrong?

I searched multiple things on sight none seemed to show the same problem. So I'm sorry if I'm not smart on finding the answer if it has been answered. I did search for about 30 min.

Code:

import java.util.Scanner;
import java.util.*;


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

     Scanner userInput = new Scanner(System.in);

        int matchOne;
        int matchTwo;
        int matchThree;
        int matchFour;

        /*
         * 
         ********** THE NUMBERS CHOSEN BY USER **********
         *
        */

        System.out.println("First number picked: ");

        if (userInput.hasNextInt()){

            int pickOne = userInput.nextInt();

        }

        System.out.println("Second number picked: ");

        if (userInput.hasNextInt()){

            int pickTwo = userInput.nextInt();

        }

        System.out.println("Third number picked: ");

        if (userInput.hasNextInt()){
            int pickThree = userInput.nextInt();    
        }

        System.out.println("Fourth number picked: ");

        if (userInput.hasNextInt()){

            int pickFour = userInput.nextInt();

        }

        /*
         * 
         ********** THE WINNING NUMBERS DRAWN **********
         *      
        */

        System.out.println("First number drawn: ");

        if (userInput.hasNextInt()){
            int drawnOne = userInput.nextInt();
        }

        System.out.println("First number drawn: ");

        if (userInput.hasNextInt()){
            int drawnTwo = userInput.nextInt();
        }

        System.out.println("First number drawn: ");

        if (userInput.hasNextInt()){
            int drawnThree = userInput.nextInt();
        }

        System.out.println("First number drawn: ");

        if (userInput.hasNextInt()){
            int drawnFour = userInput.nextInt();
        }

        /*
         ********** MATCHING NUMBERS **********
        */

Here is the problem:

        if (pickOne == drawnOne){
            System.out.println(pickOne + " is a match!");
        }

    }

}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • by the way the scope of variable `pickOne` and `drawOne` is limited to the if condition. you should declare them inside main function as a local variable. – Wasi Ahmad Dec 10 '16 at 01:31

2 Answers2

0

What's the input and the output. Give some examples. I don't see a good use of if (userInput.hasNextInt())

Do you mean like this?

import java.util.Scanner;
import java.util.*;

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

        Scanner userInput = new Scanner(System.in);

        int matchOne;
        int matchTwo;
        int matchThree;
        int matchFour;

        int pickOne;
        int pickTwo;
        int pickThree;
        int pickFour;

        int drawnOne;
        int drawnTwo;
        int drawnThree;
        int drawnFour;

        /*
         ********** THE NUMBERS CHOSEN BY USER **********
         */

        System.out.println("First number picked: ");
        pickOne = userInput.nextInt();

        System.out.println("Second number picked: ");
        pickTwo = userInput.nextInt();

        System.out.println("Third number picked: ");
        pickThree = userInput.nextInt();

        System.out.println("Fourth number picked: ");
        pickFour = userInput.nextInt();

        /*
         * 
         ********** THE WINNING NUMBERS DRAWN **********
         * 
         */

        System.out.println("First number drawn: ");
        drawnOne = userInput.nextInt();

        System.out.println("Second number drawn: ");
        drawnTwo = userInput.nextInt();

        System.out.println("Third number drawn: ");
        drawnThree = userInput.nextInt();

        System.out.println("Fourth number drawn: ");
        drawnFour = userInput.nextInt();

        /*
         ********** MATCHING NUMBERS **********
         */

        if (pickOne == drawnOne) {
            System.out.println(pickOne + " is a match!");
        }

    }

}
0

Variables defined in a block are only accessible from within the block. In your program, the scope of your pickedOne, pickedTwo, ...., drawOne, drawTwo, .... variables are limited to the if blocks where you declared them. So, if you try to use them outside those if conditions, you will get error saying, cannot find symbol.

Declare the following variables inside main() function instead of inside if conditions.

int pickOne;
int pickTwo;
int pickThree;
int pickFour;

int drawnOne;
int drawnTwo;
int drawnThree;
int drawnFour;

I highly encourage you to read about scope of variables in Java.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • Ok awesome sorry I thought i was good i haven't coded in a while and thanks for the link its already helped another problem i encountered. Have a great night and take it easy – Michael Pritchard Dec 10 '16 at 01:53