2013-10-31 95 views
0

我是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); 
} 
+1

如果您想在外部使用它们,请不要在'try'块中声明变量。 – Maroun

回答

1

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

2

你正在try块中声明幅度。

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

所以你必须声明它的尝试外:

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

对,这真的很有帮助谢谢 –

0

尝试这是在try块创建此

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

如果你返回的东西至少要初始化为'null' – Dropout

1

的变量是local variables。它们只存在于try块内,因此它们不能从外部访问。如果你在try块上面声明变量,你可以在try块内外访问它。

+0

谢谢,这是唯一的答案,提供了一些关于范围界定的额外解释。 – Geekfish

0

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

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

只是声明幅度全局变量来,我认为问题的get解决

你宣称try块内的变量,并试图访问try.that这个变量,这就是它提出错误的原因,因为找不到符号。