有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

未解析java变量/用户输入

好的,这里是: 我询问所选的号码
然后询问绘制的数字
然后,当我转到所以如果他们匹配它告诉我,pickOne无法解决,但我使用的是用户的输入,我做错了什么

我一眼就搜索了很多东西,似乎都没有发现同样的问题。因此,如果我不善于找到答案(如果答案已被回答),我很抱歉。我搜索了大约30分钟

代码:

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 **********
        */

问题出在这里:

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

    }

}

共 (2) 个答案

  1. # 1 楼答案

    块中定义的变量只能从块中访问。在您的程序中,pickedOnepickedTwodrawOnedrawTwo。。。。变量仅限于声明它们的if块。因此,如果您试图在那些if条件之外使用它们,您将得到错误消息,cannot find symbol

    main()函数中声明以下变量,而不是在if条件中声明

    int pickOne;
    int pickTwo;
    int pickThree;
    int pickFour;
    
    int drawnOne;
    int drawnTwo;
    int drawnThree;
    int drawnFour;
    

    我强烈鼓励你阅读scope of variables in Java

  2. # 2 楼答案

    输入和输出是什么。举几个例子。我看不出if(userInput.hasNextInt())有什么好用

    你是说像这样吗

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