问题描述:

I'm a really new programmer and I've been practicing to get better. I hate to ask for help with what I'm sure is a juvenile question for a class, but I cant figure it out.The error it gives is this: It gives me this error : Lab7d.java:28: error: cannot find symbol newArray[i] = scan.nextDouble(). Thanks!

import java.util.Scanner;

import java.util.Arrays;

public class Lab7d {

public static void main (String[] args){

// Lab7d, [Naqeeb Nazir], [Masc 1555]

Scanner scan = new Scanner(System.in);

int numInputs = scan.nextInt();

double[] quakes = new double[numInputs];

quakes = init(numInputs, scan.nextDouble());

getMagnitudeStats(quakes);

print(quakes);

}

public static double[] init(int a, double n) {

double[] newArray = new double[a];

for (int i = 0; i < n; i++) {

newArray[i] = scan.nextDouble();

}

return newArray;

}

public static void getMagnitudeStats(double[] myArray) {

double Maximum = 0.0;

double Minimum = 0.0;

double Average = 0.0;

int severeEarthquakes = 0;

if (Minimum > 0 && Minimum <= 10.0) Minimum = myArray[0];

else Minimum = 1.0;

if (Maximum > 0 && Maximum <= 10.0) Maximum = myArray[0];

else Maximum = 1.0;

System.out.println("Maximum: " + Maximum);

System.out.println("Minimum: " + Minimum);

System.out.println("Average: " + Average);

System.out.println("Severe earthquakes: " + severeEarthquakes);

}

public static void print(double[] myArray1) {

for (int i = 0; i < myArray1.length; i++) {

System.out.print(myArray1[i] + " ");

}

System.out.println("");

}

}

网友答案:

Your scan variable is defined in main(), but you are trying to access it in init(). That is why you get error: cannot find symbol.

You can make scan an instance variable and declare it outside the main() method.

Or do the following to create scan in init():

public static double[] init(int a, double n) {
   Scanner scan = new Scanner(System.in);
   double[] newArray = new double[a];
   for (int i = 0; i < n; i++) {
    newArray[i] = scan.nextDouble();
      }
      return newArray;
  }
网友答案:

scan is a local variable in main, you can't access it in init() here newArray[i] = scan.nextDouble();

相关阅读:
Top