我的类作业被困住了:
Create an application containing an array that stores eight integers. The application should
- display all the integers, (done)
- display all the integers in reverse order,
- display the sum of the eight integers, (done)
- display all values less than 5,
- display the lowest value, (done)
- display the highest value, (done)
- calculate and display the average, (Done)
- display all values that are higher than the calculated average value.
我必须使用(或尝试使用)数组;还必须至少使用一个循环来“遍历”(移动)数组。这也将于今晚山区标准时间 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
最佳答案
从数组中获取总和、最低和最高整数的示例循环
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;
}
编辑:最新的代码更新取得了一些进展,但可能不太满足指定的要求。了解完整的要求(例如输出格式)存在一些困难。
- 仅输出
当前示例正在显示输出(每行一个条目)。目前尚不清楚是否只需要显示输出或实际反转数组。如果唯一的要求是输出反转,大于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();
- 创建一个新的反转数组
对于需要实际反转数组的赋值来说更有意义。在这种情况下,有几种方法。也许最简单的方法是创建一个新数组并将元素反转到其中。本质上,它与上面的循环相同,但将 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));
关于java - 编写这些java数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393935/