开发者社区> 问答> 正文

调用一个名为printArray的方法,该方法打印我的随机数数组

到目前为止,我已经制作了一个随机数数组,但是我仍然坚持如何制作可以在main中调用的printarray方法。我需要在同一类中调用print数组,并打印出所有用一个空格分隔的元素

public class HunterIsaacsHw6
{
public static void main(String[] args)
{
System.out.println("Java, Online, Hunter Isaacs, hw6"); 
 // define the range 
int max = 100; 
int min = 1; 
int range = max - min + 1; 
// combining both statements in one
double doubleArray[] = new double[10]; 
// generate random numbers within 1 to 10 
for (int i = 0; i < 10; i++) {  
    doubleArray[i]  = (Math.random() * range) + min; 
} 
}
public static double printArray(doubleArray[]){
    for(double n: doubleArray){
        System.out.println(n+" ");
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-21 21:32:53 1450 0
1 条回答
写回答
取消 提交回答
  • 您的方法声明不正确,甚至没有调用它。尝试:

    public static void main(String [] args)
    {
    
        System.out.println("Java, Online, Hunter Isaacs, hw6"); 
         // define the range 
        int max = 100; 
        int min = 1; 
        int range = max - min + 1; 
        // combining both statements in one
        double doubleArray[] = new double[10]; 
        // generate random numbers within 1 to 10 
        for (int i = 0; i < 10; i++) {  
            doubleArray[i]  = (Math.random() * range) + min; 
        } 
    
        printArray(doubleArray);
    }
    public static void printArray(double doubleArray[]){
            for(double n: doubleArray){
                System.out.println(n);
            }
    }
    

    另外,没有返回任何内容,printArray因此应将其声明为无效

    我也更喜欢声明为 double[] doubleArray

    编辑

    也是System.out.println(n);足够的,不需要添加空格

    回答来源:Stack Overflow

    2020-03-21 21:33:48
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载