编写这些java数组的最佳方法是什么?

时间:2016-04-04 03:00:09

标签: java

我被困在班上工作:

  

创建一个包含存储八个整数的数组的应用程序。应用程序

     
      
  1. 显示所有整数,(完成)
  2.   
  3. 以相反的顺序显示所有整数,
  4.   
  5. 显示八个整数的总和,(完成)
  6.   
  7. 显示小于5的所有值,
  8.   
  9. 显示最低值,(完成)
  10.   
  11. 显示最高值,(完成)
  12.   
  13. 计算并显示平均值,(完成)
  14.   
  15. 显示高于计算平均值的所有值。
  16.   

我必须使用(或尝试使用)数组;还必须使用至少一个循环来“遍历”#34; (穿过)数组。这也是今晚23:59山区标准时间的到来

我做错了什么?

package numberlistdemo;

import java.util.Arrays;

public class NumberListDemo 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
             int n[]= {1,2,3,4,5,6,7,8};
    int lowest = 1000;
    int highest = 0;
    int sum = 0;
    int five = 0;
    int OverF = 0;
    int rev = 0;
    int OverAve= 0;

        for (int i=0;i<n.length;i++)

        {

            int cur = n[i];
            if (cur < lowest) lowest = cur;
            if (cur > highest) highest = cur;
             sum += cur;

        }
         double ave = sum / n.length;

         for (int i=0;i<n.length;i++)
         {
            int cur = n[i];
            if (cur > ave) OverAve = cur;
         }

       for (int i=0;i<n.length;i++)
       {

           int LowF = n[i];
           if (LowF < 5) five = LowF;
           if (LowF > 5) OverF  = LowF;
       }



            //3
            System.out.println("Total of the Array is " + sum );
            //1
            System.out.println("The number we are using are " + Arrays.toString(n));
            //4
            System.out.println("All values lower the 5 are " + five );
            ////2
             for (int counter=n.length - 1; counter >= 0; counter--)
        {
           System.out.println("The reverse order of the numbers are " + (n[counter]));
        }         
             //5
            System.out.println("The lowest value is " + lowest);
            //6
            System.out.println("The highest value is "+ highest);
            //7
            System.out.println("The average is " + ave);     
            //8
            System.out.println("All numbers higher than the average are: " + OverAve);


    }
}

我明白了

Total of the Array is 36
The number we are using are [1, 2, 3, 4, 5, 6, 7, 8]
All values lower the 5 are 4
The reverse order of the numbers are 8
The reverse order of the numbers are 7
The reverse order of the numbers are 6
The reverse order of the numbers are 5
The reverse order of the numbers are 4
The reverse order of the numbers are 3
The reverse order of the numbers are 2
The reverse order of the numbers are 1
The lowest value is 1
The highest value is 8
The average is 4.0
All numbers higher than the average are: 8

3 个答案:

答案 0 :(得分:1)

采样循环以从数组中获得总和,最低和最高整数

int lowest = 1000;
int highest = 0;
int sum = 0;
for (int i = 0; i < array.length; ++i) {
  int cur = array[i];
  if (cur < lowest) lowest = cur;
  if (cur > highest) highest = cur;
  sum += cur;
}

编辑:最新的代码更新取得了一些进展,但可能不太符合指定的要求。知道完整要求(例如输出格式)存在一些困难。

  1. 仅输出
  2. 当前示例显示输出(每行一个条目)。目前尚不清楚是否只需要显示输出或实际反转数组。如果唯一的要求是输出反转,大于5且大于8的数字,则以下三个循环将起作用。其中一个已经在建议的代码中了。

    System.out.println("The reversed array is: ");
    for (int counter < n.length - 1; counter >= 0; counter--) {
        System.out.print(n[counter] + " ");
    }
    System.out.println();
    

    请注意,这两个应该在方法

    final int lessThan = 5;
    System.out.println("The numbers in the array less than " + lessThan + " are: ");
    for (int i = 0; i < n.length; ++i) {
      if (n[i] < lessThan) {
        System.out.print(n[i] + " ");
      }
    }
    System.out.println();
    
    System.out.println("The numbers in the array greater than the mean " + ave + " are: ");
    for (int i = 0; i < n.length; ++i) {
      if (n[i] > ave) {
        System.out.print(n[i] + " ");
      }
    }
    System.out.println();
    
    1. 创建一个新的反转数组
    2. 对于要求实际反转数组的赋值更有意义。在这种情况下,有几种方法。可能最简单的方法是创建一个新数组并将元素反转到其中。本质上它是与上面相同的循环,但将n的元素放入数组而不是将它们输出到屏幕。 (注意:@Debosmit提出了一个不需要额外索引变量的例子。)

      int[] reversed = new int[n.length];
      int idx = 0;
      for (i = n.length - 1; i >= 0; --i) {
         reversed[idx++] = n[i]];
      }
      

      稍后可以使用与显示所有原始条目相同的输出方法:

      System.out.println("The reversed array is: " + Arrays.toString(reversed));
      

答案 1 :(得分:0)

错误讯息是什么? 由于您没有使用大括号{ },因此您的局部变量int i将仅存在于这些行中:

 for (int i=0;i<n.length;i++)
         sum = sum + n[i];

而不是这些:

double rev = n.length - i - 1;

boolean l = (n[i] < 5);

答案 2 :(得分:0)

啊!嗯......我会做出贡献。以下是如何反转...

// note this will change the original array
for(int i = 0 ; i < n.length ; i++) {
    int t = n[i];
    n[i] = n[n.length - 1 - i];
    n[n.length - 1 - i] = t;
}

// this will not change the original array
int[] newArr = new int[n.length];
for(int i = 0 ; i < n.length ; i++) {
    newArr[n.length - 1 - i] = n[i];
}
// you can now return `newArr` or something since it is `n` reversed

现在查看高于计算平均值的所有值。我想你已经有了这些数字的总和。让我们称之为sumavg = sum/n.length

for(int i = 0 ; i < n.length ; i++) {
    if(n[i] > avg)
        System.out.println(n[i]);
}

所有小于5的数字。

for(int i = 0 ; i < n.length ; i++) {
    if(n[i] < 5)
        System.out.println(n[i]);
}

将来,在尝试自己做之前,请不要带着你的作业到这里来。谢谢! :)