【问题标题】:Cannot find Symbol - Variable, despite the variable being declared找不到符号 - 变量,尽管声明了变量
【发布时间】:2016-04-13 19:09:09
【问题描述】:

numThrows 变量在 main 方法中使用时引发变量未找到的错误。即使我在其中一种方法中声明它。 我在 void 提示方法中使用声明变量。该程序旨在使用随机坐标计算 Pi,然后使用公式在用户给定的尝试次数上估计 pie。

import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;
public class Darts




 public static void prompt()
 {
     Scanner in = new Scanner(System.in);
    System.out.println("How many throws per trial would you like to do?: ");
    int numThrows = in.nextInt();
    System.out.println("How many trials would you like to do?: ");
    int numTrials = in.nextInt(); 



    }


 public static double[] randomX( int numThrows)
 {
     int darts = 0;
     int i = 0;
     double[] cordX = new double[numThrows];
     while(darts <= numThrows)
     {
         cordX[i] = Math.random();
         i++;
        }
     return cordX;
    }
 public static double[]randomY(int numThrows)
 {
     int darts = 0;
     int i = 0;
     double [] cordY = new double[numThrows];
     while(darts <= numThrows)
     {
         cordY[i] = Math.random();
         i++;
        }
     return cordY;


    }
 public static void getHits(int numThrows, double[] cordX, double[] cordY)
 {
     int ii = 0;
     int i = 0;
     double hits = 0;
     double misses = 0;
     for(i = 0; i <= numThrows; i++)
      {
     if( Math.pow(cordX[ii],2) + Math.pow(cordY[ii],2) <= 1)
     {
         hits++;
         ii++;

        }
        else{
            misses++;
        }
    }

    }
 public static double calcPi(int misses, int hits)
{
    int total = hits + misses;
    double pi = 4 * (hits / total);

}
// public static void print(double pi, int numThrows)
// {

   //  System.out.printf("  %-7s         %3.1f            %7s\n", "Trial[//numtrial]: pi = "

 //   }


   public static void main(String[] args)throws IOException
    {
     prompt();
     double[] cordX = randomX(numThrows);
     double[] cordY = randomY(numThrows);
     gethits();
     double pi = calcPi(misses, hits);

}

}

【问题讨论】:

    标签: java variables methods symbols cannot-find-symbol


    【解决方案1】:

    尝试删除方法prompt()它没有使用,并将他的块放在main方法中。

     public static void main(String[] args)throws IOException
      {
       Scanner in = new Scanner(System.in);
        System.out.println("How many throws per trial would you like to do?: ");
        int numThrows = in.nextInt();
        System.out.println("How many trials would you like to do?: ");
        int numTrials = in.nextInt(); 
        double[] cordX = randomX(numThrows);
        ...
    

    【讨论】:

      【解决方案2】:

      如果numThrows 在另一个函数中声明,那么它的scope 不会扩展到主方法。

      相反,如果您想在 main 方法和另一个方法中使用它,请将其设为类实例。

      例如:

      class SomeClass {
          public static int numThrows = 2;
          public static void test() {
              numThrows = 4; // it works!
          }
          public static void main(String[] args) {
              System.out.println(numThrows); // it works!
          }
      }
      

      因此,它的范围将扩展到类的所有成员,而不仅仅是方法。

      【讨论】:

        【解决方案3】:

        numThrows 是提示方法的实例变量。如果您想做我认为您想做的事情,请将 numThrows 设为任何方法之外的静态变量。

        看起来像这样:

        public class Darts {
            public static int numThrows
            public static int numTrials
        

        这些变量可以被任何方法引用。这应该可以解决它。

        【讨论】:

          猜你喜欢
          • 2018-08-30
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-14
          • 2016-07-01
          • 2016-03-27
          相关资源
          最近更新 更多