ForLoop / Array问题

时间:2015-07-23 02:50:42

标签: java arrays

我在弄清楚该项目时应该做些什么(我的所有项目通常都是一样的)。我只是想知道我是否也朝着正确的方向前进,我不知道如何将浮点数存储在int double数组中。谢谢

问题& SampleOutput

写,提示用户输入一个整数的Java程序。使用整数作为新double数组的大小。使用for循环提示用户输入每个元素的浮点数 数组,并将数字存储在数组中。使用第二个for循环,计算所有数字的平均值并打印出来。

您的输出应如下所示:

  

您输入多少个数字:5

     

输入小数值:21.2

     

输入小数值:3.7

     

输入小数值:10.5

     

输入小数值:2.6

     

输入小数值:101.123

     

的平均值是27.824599999999997

我目前的代码:

import java.util.*;

public class Loops {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print(" How many numbers will you enter: ");
int numberOfTimes = input.nextInt();


int[][] Array = new int[numberOfTimes][];

for(int i = 0; i < Array.length; i--)
{
    System.out.print("Enter a decimal value: ");
    float value = input.nextInt();
    Array[i][



    {

    }
}

}
}

PS:我为糟糕的格式化道歉。 Stack的新手。

3 个答案:

答案 0 :(得分:0)

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print(" How many numbers will you enter: ");
    int numberOfTimes = input.nextInt();


    float[] input = new int[numberOfTimes];

    for(int i = 0; i < numberOfTimes; i++) {
        System.out.print("Enter a decimal value: ");
        float value = input.nextInt();
        Array[i] = value;
    }
}

答案 1 :(得分:0)

1)您需要一个double数组而不是二维int数组来存储double

2)使用input.nextDouble();进行double输入。

3)for(int i = 0; i < Array.length; i--)会导致i减少负数,这会在ArrayIndexOutofBound处产生Array[i]例外。

4)还有其他一些错误,请尝试以下代码。

试试这个:

import java.util.*;

public class Loops {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print(" How many numbers will you enter: ");
        int numberOfTimes = input.nextInt();

        double[] Array = new double[numberOfTimes];
        double sum = 0;
        double average = 0;

        for (int i = 0; i < numberOfTimes; i++) {
            System.out.print("Enter a decimal value: ");
            double value = input.nextDouble();
            Array[i] = value;
            sum += value;
        }
        average = sum / numberOfTimes;
        System.out.print("The average is: " + average);
    }
}

输出:

  

您输入多少个数字:5

     

输入小数值:21.7

     

输入小数值:3.7

     

输入小数值:10.5

     

输入小数值:2.6

     

输入小数值:101.123

     

平均值为:27.924599999999998

答案 2 :(得分:0)

爵士

你提出的主要错误是考虑加倍为两。 'double'也是一个数据类型,就像整数和浮点数一样。因此,您只需要一维数组来存储“double”类型的值。

其次,我建议你尝试自己完成一次代码。如果您在编译或输出错误时遇到问题,请随时在线发布代码。

只有一个'for'循环可以实现所需的结果,但为了清晰起见,我为'循环提供了代码2'。

这是代码 -

swap(minheap[i], minheap[j]);
maxheap[minheap[i].second].second = i;
maxheap[minheap[j].second].second = j;

所需的输出将是 -

import java.util.Scanner;

public class AverageNumber {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("How many numbers will you enter: ");
int numberOfTimes = input.nextInt();

double AverageMean = 0;
double[] Array = new double[numberOfTimes];

// Doing the first loop as suggested
for(int i = 0; i < numberOfTimes; i++)
{
    System.out.print("Enter a decimal value: ");
    Array[i]  = input.nextDouble();
}

// Doing the second loop as suggested
for(int i = 0; i < numberOfTimes; i++)
{
    AverageMean = AverageMean + Array[i];
}

System.out.println("The average is " + AverageMean/5);

}

}

希望有所帮助......