为什么我的Scan.nextDouble给我一个java错误?

时间:2015-11-28 04:15:23

标签: java arrays

我是一个非常新的程序员,而且我一直在努力变得更好。我讨厌寻求帮助,我确定这是一个课程的少年问题,但我无法弄明白。它给出的错误是:它给了我这个错误:Lab7d.java:28:错误:找不到符号newArray [i] = scan.nextDouble()。谢谢!

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

   }

2 个答案:

答案 0 :(得分:2)

scanmain中的本地变量,您无法在init() newArray[i] = scan.nextDouble();

中访问scan

答案 1 :(得分:0)

您的main()变量已在init()中定义,但您尝试在error: cannot find symbol中访问该变量。这就是你得到scan的原因。

您可以将main()设为实例变量,并在scan方法之外声明它。

或者执行以下操作在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; }

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>  // For string library functions
#include <cstdlib>  // For exit() function
using namespace std;

// MyString class: An abstract data type for handling strings
class MyString
{
private:
char *str;
int len;
public:
// Default constructor.
MyString()
 { 
   str = 0; 
   len = 0;
 }

// Convert and copy constructors. 
MyString(char *);
MyString(MyString &);

// Destructor. 
~MyString()
 { 
   if (len != 0)
      delete [] str;
      str = 0;
      len = 0;
 }

// Various member functions and operators.   
int length() { return len; }
char *getValue() { return str; };
MyString operator+=(MyString &);
MyString operator+=(const char *);
MyString operator=(MyString &);
MyString operator=(const char *);
bool operator==(MyString &);
bool operator==(const char *);
bool operator!=(MyString &);
bool operator!=(const char *);
bool operator>(MyString &);
bool operator>(const char *);
bool operator<(MyString &);
bool operator<(const char *);
bool operator>=(MyString &);
bool operator>=(const char*);
bool operator<=(MyString &);
bool operator<=(const char *);
MyString operator [](MyString *);

// Overload insertion and extraction operators.
friend ostream &operator<<(ostream &, MyString &);
friend istream &operator>>(istream &, MyString &);
};
#endif