创建一个数组列表 [英] Creating an Array List

查看:127
本文介绍了创建一个数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用我目前的计划,并在分离averging功能的方法出来的主要方法的一面。我想用扫描仪抢到号码存储到一个数组列表,然后用我的平均数方法,以便抓住那些数和平均数字加在一起。然后输出平均取代电流的System.out.println你的平均..

请帮我说明这一点。我无法理解这一切是如何走到一起。

 进口java.util.Scanner中;类programTwo {公共静态无效的主要(字串[] args){
    扫描程序扫描=新的扫描仪(System.in);
    双总和= 0;
    诠释计数= 0;
    的System.out.println(输入你的号码被平均);
    字符串输入= scan.nextLine();    而(!inputs.contains(Q)){
        扫描仪SCAN2 =新的扫描仪(输入); //创建一个新的扫描仪我们输入单行         {
            总和+ = scan2.nextDouble();
            数+ = 1;
            的System.out.println(请输入其他号码或preSS Q代表你的平均);
        }        如果(计数== 21)
        {
            的System.out.println(你输入了太多的数字失败!);
            返回;
        }
        输入= scan.nextLine();
    }
    的System.out.println(您的平均值是:+(总和/计数));
}
}


解决方案

  //这里增加了一个进口
进口的java.util.ArrayList;
进口java.util.Scanner中;类programTwo
{
    //主要区别是平均计算的方法内完成的,而不是主
    公共静态无效的主要(字串[] args)
    {
        扫描程序扫描=新的扫描仪(System.in);
        ArrayList的<双> myArr,该=新的ArrayList<双>();
        双总和= 0;
        诠释计数= 0;
        的System.out.println(输入一个数字,要平均:);
        字符串输入= scan.nextLine();    而(!inputs.contains(Q))//输入,直到用户不再愿意给输入
    {        如果(计数== 21)
        {
            打破; //这个命令在这里跳出输入回路中,如果有21
        }        扫描仪SCAN2 =新的扫描仪(输入); //创建一个新的扫描仪我们输入单行
        myArr.add(scan2.nextDouble()); //简单地添加到数组列表
        数+ = 1;
        的System.out.println(请输入其他号码或preSS Q代表你的平均);        输入= scan.nextLine();
    }
    双平均= calculate_average(myArr,该); //转到方法计算平均预期的双重归还
    的System.out.println(您的平均值是:+平均值);
}私有静态双calculate_average(ArrayList的<双> myArr,该)//方法定义
{
    双总和= 0.0;
    对于(双号:myArr,该)// for循环,通过一个数组列表迭代
    {
        总和+ =号; //将所有的号码连成一个总和
    }
    返回总和/ myArr.size(); //返回由数字数除以相加的数组列表
}
}

这应该帮助。好运:)

I would like to take my current program and separate the averging function in to a method out side of the main method. I would like to store the numbers grabbed with the scanner in to an array list and then use my averaging method to grab those number and average the numbers together. then Output the average in place of the current System.out.println your average is..

Please help me illustrate this. I am having trouble understanding how all this comes together.

import java.util.Scanner;

class programTwo {

public static void main (String[] args) {
    Scanner scan = new Scanner(System.in);
    double sum = 0;
    int count = 0;
    System.out.println ("Enter your numbers to be averaged:");
    String inputs = scan.nextLine();

    while (!inputs.contains("q")) {
        Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input

         {
            sum += scan2.nextDouble();
            count += 1;
            System.out.println("Please enter another number or press Q for your average");
        } 

        if(count == 21)
        {
            System.out.println("You entered too many numbers! Fail.");
            return;
        }
        inputs = scan.nextLine();
    }
    System.out.println("Your average is: " + (sum/count));
}
}

解决方案

//added an import here
import java.util.ArrayList;
import java.util.Scanner;

class programTwo
{
    //main difference is the average calculation is done within a method instead of main
    public static void main( String[] args )
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        double sum = 0;
        int count = 0;
        System.out.println("Enter a number to be averaged:");
        String inputs = scan.nextLine();

    while (!inputs.contains("q")) //input until user no longer wants to give input
    {

        if (count == 21)
        {
            break; //this command here jumps out of the input loop if there are 21
        }

        Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input
        myArr.add(scan2.nextDouble()); //simply adding to the array list
        count += 1;
        System.out.println("Please enter another number or press Q for your average");

        inputs = scan.nextLine();
    }
    Double average = calculate_average(myArr); //go to method calculate average, expect a double to be returned
    System.out.println("Your average is: " + average); 
}

private static Double calculate_average( ArrayList<Double> myArr ) //method definition
{
    Double Sum = 0.0; 
    for (Double number: myArr) //for loop that iterates through an array list
    {
        Sum += number; //add all the numbers together into a sum
    }
    return Sum / myArr.size(); //return the sum divided by the number of numbers in the array list
}
}

This should help. Best of luck :)

这篇关于创建一个数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆