[英]Cannot find symbol - variable
我是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块内声明了幅度。
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);
}
您必须在try-catch之外定义变量。 您想在try-catch之外使用的其他变量也是如此。
在try块中创建的变量是local variables
。 它们仅存在于try块内部,因此无法从外部访问它们。 如果在try块上方声明变量,则可以在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);
}
在try块之外声明它,即使它为null。
Double magnitude = null;
try{
// ...
magnitude = Double.parseDouble(scanner.nextLine());
// ...
}catch{
// ...
}
return magnitude;
只是在全球范围内声明幅度变量,我认为问题可以解决
您在try块中声明了该变量,并尝试在try之外访问该变量。这就是由于找不到符号而引发错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.