我是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);
}
答案 0 :(得分:2)
你在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);
}
答案 1 :(得分:1)
您必须在try-catch之外定义变量。您希望在try-catch之外使用的其他变量也是如此。
答案 2 :(得分:1)
在try块中创建的变量是local variables
。它们只存在于try块中,因此无法从外部访问它们。如果在try块上方声明变量,则可以在try块内外访问它。
答案 3 :(得分:0)
试试这个
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);
}
答案 4 :(得分:0)
在try块之外声明它,即使它是null ..
Double magnitude = null;
try{
// ...
magnitude = Double.parseDouble(scanner.nextLine());
// ...
}catch{
// ...
}
return magnitude;
答案 5 :(得分:0)
只是在全局声明幅度变量我认为问题得到解决
你在try块中声明了这个变量,并试图在try.it之外访问该变量,这就是它因为无法找到符号而引发错误的原因。