使用参数调用方法

时间:2013-12-11 00:39:09

标签: java arrays methods

  

写下列方法:
  13.一个名为loadScores的方法,它将传递一个数组的大小,提示用户输入适当数量的浮点数,然后返回加载的数组。

我编写了这个方法,但是当我尝试调用它时问题出现了。这就是我所拥有的:

public class ArrayHandout {
  public static void main(String[] args) {
     int size = loadScores(size);
     double[] scoresArray = loadScores(size);
  } // end main

  public static double[] loadScores(int size) {
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?");
    size = input.nextInt();
    double[] scoresArray = new double[size]; 

    int i;
    for(i = 0; i < scoresArray.length; i++) {
        System.out.println("Please enter a score:");
        scoresArray[i] = input.nextDouble(); 
    }// end for

    System.out.println(scoresArray[i]); 
    return scoresArray;   
  } // end loadScores  
} // end class

我已经改变了一些事情,试图纠正我在编写原始代码时遇到的一些错误,很遗憾甚至不记得这些更改是什么,或者他们是否修复了问题,因为我无法得到它现在编译。我知道当我可以获得原始编译时我遇到的问题是它只打印数组中的第一个数字而不是打印整个数组。就像我说的,我不知道这个问题是否已经纠正,因为现在我尝试编译时收到以下错误消息:

  

找到1个错误:       文件:C:\ Users \ HiTechRedneck \ Desktop \ Fall 2013 \ PlayingwithJava \ ArrayHandout.java [line:6]       错误:不兼容的类型         必需:int         发现:double []

我知道size需要在main中声明,因为之前我收到了“找不到可变大小”错误,但我认为我正在尝试声明{{1}由于我声明的变量也是方法中的参数,因此int size将它抛弃。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您的第int size = loadScores(size)行不正确。 loadScores的返回类型为double[],因此您需要指定返回值。

只需删除一行,一切都应按预期工作。

答案 1 :(得分:0)

int size = loadScores(size);抛出错误,因为loadScores返回double类型的数组。

由于任务只需要一个代码片段,你可以给大小一个任意值并传入它。或者你可以在main中删除大小并只传递一个数字:

public static void main(String[] args) {
    double[] scoresArray = loadScores(5);
}

编辑:还值得注意的是,for循环之后的print语句将抛出一个ArrayIndexOutOfBoundsException,因为我现在大于数组的长度。如果你想打印scoresArray的内容,你需要另一个循环来遍历每个元素。

第二次编辑:如果您提示用户输入大小,您应该在main方法中运行提示,然后将其传递给loadScores()。

答案 2 :(得分:0)

在函数中读取函数之前调用函数之前根本不需要大小 你的声明可能是:

public static double[] loadScores()

并致电

loadScores()

主要。例如:

 public class ArrayHandout {   
   public static void main(String[] args) {
     double[] scoresArray = loadScores();  
     //do whatever you want here or change declaration of 
     //the function to void and do not return anything
   }  

   public static double[] loadScores() {
     Scanner input= new Scanner(System.in); 

     System.out.println("How many scores would you like to enter?");
     int size = input.nextInt();
     double[] scoresArray = new double[size]; 

     int i;
     for(i = 0; i < scoresArray.length; i++) {
       System.out.println("Please enter a score:");
       scoresArray[i] = input.nextDouble(); 
     }// end for

     System.out.println(scoresArray[i]); 
     return scoresArray;      
  }
}

如果你不使用返回的数组,最好这样做:

public class ArrayHandout {   
  public static void main(String[] args) {
    loadScores(); 
  }  

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

    System.out.println("How many scores would you like to enter?");
    int size = input.nextInt();
    double[] scoresArray = new double[size]; 

    int i;
    for(i = 0; i < scoresArray.length; i++) {
      System.out.println("Please enter a score:");
      scoresArray[i] = input.nextDouble(); 
    }// end for

    System.out.println(scoresArray[i]);   
  }
}

(当一个方法没有返回任何东西时你必须通过在其签名中使用void关键字来指定它) - 你还需要验证但我只回答了特定的问题 - 如果不兼容你的程序仍然会失败给出了值。