【问题标题】:Cannot find symbol - variable找不到符号 - 变量
【发布时间】:2023-04-02 12:15:02
【问题描述】:

我是 Java 新手,我正在尝试获取用户输入,将输入的每一行存储为变量,然后返回每个值,以便可以将其传递到其他地方。当我尝试编译时,它告诉我它找不到变量幅度。我假设它也不会找到其他人。 我猜这是因为我已经在“try”中声明了变量,但不知道如何获取它以便 return 语句接受它们。代码如下:

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

【问题讨论】:

  • 如果你想在try块之外使用变量,不要在它之外声明它们。

标签: java variables compilation return


【解决方案1】:

您在 try 块内声明量级。

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

所以你必须在 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);   
}

【讨论】:

    【解决方案2】:

    您必须在 try-catch 之外定义变量。您想在 try-catch 之外使用的其他变量也是如此。

    【讨论】:

      【解决方案3】:

      在 try 块中创建的变量是local variables。它们只存在于 try 块内,因此无法从外部访问。如果在 try 块上方声明变量,则可以在 try 块内外访问它。

      【讨论】:

      • 谢谢,这是对范围界定提供一些额外解释的唯一答案。
      【解决方案4】:

      试试这个

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

      【讨论】:

      • 如果你返回一些东西,它必须至少初始化为null
      【解决方案5】:

      在 try 块之外声明它,即使它是 null..

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

      【讨论】:

        【解决方案6】:

        只需全局声明幅度变量我认为问题得到解决

        您在 try 块中声明了该变量并尝试在 try 之外访问该变量。这就是它引发错误的原因,因为找不到符号。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-14
          • 2016-07-01
          • 2016-03-27
          • 2010-10-12
          • 2012-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多