简体   繁体   English

找不到符号-变量

[英]Cannot find symbol - variable

I'm new to Java, and I'm trying to get user input, store each line of input as a variable and then return each value so that it can be passed on somewhere else. 我是Java的新手,我正在尝试获取用户输入,将输入的每一行存储为变量,然后返回每个值,以便可以将其传递到其他地方。 When I try and compile it is telling me that it can't find the variable magnitude. 当我尝试编译时,它告诉我找不到可变的幅度。 I'm assuming it won't find the others either. 我假设它也找不到其他人。 I'm guessing that this is because I've declare the variables inside of the "try" but don't know how to get it so that the return statement accepts them. 我猜这是因为我已经在“ try”中声明了变量,但是不知道如何获取变量,以便return语句接受它们。 Code is as follows: 代码如下:

 public Earthquake userAddEarthquake()
    {

        Scanner scanner = new Scanner(System.in);
        try{
            // convert the string read from the scanner into Integer type
            System.out.println("Please Enter An Earthquake Magnitude: ");
            Double magnitude = Double.parseDouble(scanner.nextLine());
            System.out.println("Please Enter The Earthquakes Latitude Position: ");
            scanner = new Scanner(System.in);
            Double positionLatitude = Double.parseDouble(scanner.nextLine()); 
            System.out.print("Please Enter The Earthquakes Longitude Position: ");
            scanner = new Scanner(System.in);
            Double positionLongitude = Double.parseDouble(scanner.nextLine()); 
            System.out.print("Please Enter The Year That The Earthquake Occured: ");
            scanner = new Scanner(System.in);
            int year = Integer.parseInt(scanner.nextLine()); 
            System.out.println("Magnitude = " + magnitude);
    }  

    catch(NumberFormatException ne){
            System.out.println("Invalid Input");
        }
        finally{
            scanner.close();

        }

     return new Earthquake(magnitude, positionLatitude, positionLongitude, year);   
}

You are declaring magnitude inside the try block. 您在try块内声明了幅度。

try {
    Double magnitude
    //magnitude will only be visible inside try block
}

so you have to declare it outside the try: 因此,您必须在尝试之外声明它:

public Earthquake userAddEarthquake() {

    Scanner scanner = new Scanner(System.in);
    Double magnitude = Double.MIN_VALUE; //with a default value
    try{
        // convert the string read from the scanner into Integer type
        System.out.println("Please Enter An Earthquake Magnitude: ");
        magnitude = Double.parseDouble(scanner.nextLine());
        System.out.println("Please Enter The Earthquakes Latitude Position: ");
        scanner = new Scanner(System.in);
        Double positionLatitude = Double.parseDouble(scanner.nextLine()); 
        System.out.print("Please Enter The Earthquakes Longitude Position: ");
        scanner = new Scanner(System.in);
        Double positionLongitude = Double.parseDouble(scanner.nextLine()); 
        System.out.print("Please Enter The Year That The Earthquake Occured: ");
        scanner = new Scanner(System.in);
        int year = Integer.parseInt(scanner.nextLine()); 
        System.out.println("Magnitude = " + magnitude);
}  

catch(NumberFormatException ne){
        System.out.println("Invalid Input");
    }
    finally{
        scanner.close();

    }

 return new Earthquake(magnitude, positionLatitude, positionLongitude, year);   
}

You have to define the variable outside of your try-catch. 您必须在try-catch之外定义变量。 Same goes for the other variables you want to use outside the try-catch. 您想在try-catch之外使用的其他变量也是如此。

The variables which are created in the try block are local variables . 在try块中创建的变量是local variables They just exist inside the try block and so they are not accessible from outside. 它们仅存在于try块内部,因此无法从外部访问它们。 If you declare the variable above the try block, you can access it in and outside of the try block. 如果在try块上方声明变量,则可以在try块内外访问它。

try this 尝试这个

  Double magnitude=null ;
     Double positionLatitude=null;
     Double positionLongitude=null;
    int year;
    try{
                // convert the string read from the scanner into Integer type
                System.out.println("Please Enter An Earthquake Magnitude: ");
              magnitude  = Double.parseDouble(scanner.nextLine());
                System.out.println("Please Enter The Earthquakes Latitude Position: ");
                scanner = new Scanner(System.in);
              positionLatitude = Double.parseDouble(scanner.nextLine()); 
                System.out.print("Please Enter The Earthquakes Longitude Position: ");
                scanner = new Scanner(System.in);
             positionLongitude = Double.parseDouble(scanner.nextLine()); 
                System.out.print("Please Enter The Year That The Earthquake Occured: ");
                scanner = new Scanner(System.in);
                year = Integer.parseInt(scanner.nextLine()); 
                System.out.println("Magnitude = " + magnitude);
        }  

Declare it outside of the try block, even if it's null.. 在try块之外声明它,即使它为null。

Double magnitude = null;
try{
    // ...
    magnitude = Double.parseDouble(scanner.nextLine());
    // ...
}catch{
    // ...
}
return magnitude;

just declare magnitude variable globally i think problem get solves 只是在全球范围内声明幅度变量,我认为问题可以解决

you declared that variable inside the try block and trying to access that variable out side of try.that is the reason it is raising the error as cannot find symbol. 您在try块中声明了该变量,并尝试在try之外访问该变量。这就是由于找不到符号而引发错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM