【问题标题】:How to create a static array that has a length defined by user input?如何创建一个长度由用户输入定义的静态数组?
【发布时间】:2013-12-23 08:26:24
【问题描述】:

我有一个程序,其中用户输入了一些成绩并将其存储到数组中,然后程序使用该数组来计算各种值。

我正在使用多种方法,所以我想定义一个静态数组,所有方法都可以引用它并保存成绩。但是我希望它的长度等于输入的等级数。

public class GradeStatistics {
static int numGrades, sum = 0;
static int[] grades = new int[numGrades];

public static void main(String args[]){
    readGrades();
    calcSum();

readGrades() 方法找出将有多少个等级并将其分配给 numGrades

public static void readGrades(){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter the number of grades: ");
    numGrades = input.nextInt();

有没有办法让用户在定义静态数组之前输入等级数?

【问题讨论】:

  • 您使用数组而不是数组列表有什么特别的原因吗?

标签: java arrays variables static


【解决方案1】:

你可以定义静态数组,但不能初始化它:

static int[] grades;

然后就可以调用readGrades()方法,在里面初始化grades数组:

public static void readGrades(){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter the number of grades: ");
    numGrades = input.nextInt();
    grades = new int[numGrades];
}

【讨论】:

    【解决方案2】:

    声明字段时,您不必初始化字段。初始化可以在之后进行,并且可以多次重新初始化变量。所以你只需要

    static int[] grades; // declaration. The field is implicitely initialized to null.
    
    public static void readGrades(){
        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter the number of grades: ");
        int numGrades = input.nextInt();
        grades = new int[numGrades]; // initialization
    

    请注意,我将numGrades 设为局部变量而不是静态字段。没有理由将其设为静态字段。成绩数量可通过grades.length获得。

    【讨论】:

      【解决方案3】:

      在声明成绩数组时,您不必指定它。

      static int[] grades;
      

      然后在你阅读分数时分配它。

      public static void readGrades(){
      Scanner input = new Scanner(System.in);
      
      System.out.print("Enter the number of grades: ");
      numGrades = input.nextInt();
      grades = new int[numGrades];
      

      【讨论】:

        【解决方案4】:

        我会像这样实现readGrades 方法

        public static int[] readGrades(){
           Scanner input = new Scanner(System.in);
        
           System.out.print("Enter the number of grades: ");
           numGrades = input.nextInt();
           int[] grades = new int[numGrades];
           // read the grades
           return grades;
        }
        

        因为如果一个名为 readGrades 的方法也返回一些东西,它会更加直观和封装。

        【讨论】:

          【解决方案5】:

          你可以在你的静态方法中初始化数组:readGrades

          像这样:

          public class GradeStatistics {
            static int numGrades, sum = 0;
            static int[] grades = null;
          
          public static void main(String args[]){
            readGrades();
            calcSum();
          }
          
          
           public static void readGrades(){
              Scanner input = new Scanner(System.in);
          
              System.out.print("Enter the number of grades: ");
              numGrades = input.nextInt();
              grades = new int[numGrades];
             }
          }
          

          【讨论】:

            【解决方案6】:

            这样宣布你的成绩

            static int[] grades = null;
            

            并像下面这样初始化成绩。

            public static void readGrades(){
            
                   numGrades = input.nextInt();
                   grades  =new int[numGrades];
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-03-14
              • 1970-01-01
              相关资源
              最近更新 更多