英文:
How to find the smallest element in an user inputted array? Why the smallest element is displayed zero?
问题
以下是翻译好的代码部分:
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入元素的数量:");
int length = input.nextInt();
int[] num = new int[length];
System.out.println("请输入 " + length + " 个数组元素:");
int sum = 0;
int large, small;
large = small = num[0];
for (int i = 0; i < length; i++) {
num[i] = input.nextInt();
sum = sum + num[i];
}
for (int i = 0; i < length; ++i) {
if (num[i] < small) {
small = num[i];
}
if (num[i] > large) {
large = num[i];
}
}
double avg = sum / length;
System.out.println("总和为:" + sum);
System.out.println("平均值为:" + avg);
System.out.println("最小元素为:" + small);
System.out.println("最大元素为:" + large);
}
}
英文:
The code displays the sum, average, and largest element. It doesn't display the smallest element as the output is always zero. How do I display the smallest element in the array?
import java.util.Scanner;
public class Average {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int length = input.nextInt();
int[] num = new int[length];
System.out.println("Enter the "+ length + " array elements:");
int sum = 0;
int large,small;
large =small = num[0];
for (int i=0; i<length;i++) {
num[i] = input.nextInt();
sum = sum+ num[i];
}
for (int i=0; i<length; ++i) {
if (num[i]<small) {
small = num[i];
}
if(num[i]> large) {
large = num[i];
}
}
double avg = sum/length;
System.out.println("The sum is "+ sum);
System.out.println("The average is "+ avg);
System.out.println("The smallest element is "+ small);
System.out.println("The largest element is "+ large);
}
}
答案1
得分: 0
是的,这是因为默认情况下,您的字段 small 等于 0。
现在让我们逐步看一下您的 if 语句
if (num[i] < small) {
small = num[i];
}
以数字 22、11、6 为例:
所以第一步是 num[0] < 0,为什么是 0,因为之前提到 small = 0 默认值
第二步是 num[1] < 0,small 仍然保持为 0
第三步是 num[2] < 0,small 仍然保持为 0
您忽略了什么,您需要在 for 循环的第一次迭代中为 small 赋值,例如:
for (int i=0; i<length; ++i) {
if(i == 0){
small = num[i];
}
if (num[i] < small) {
small = num[i];
}
if(num[i] > large) {
large = num[i];
}
}
现在您的程序应该可以正常工作
英文:
Yea, it's happening because by default your field small is equal to 0.
And now let's look step by step your if statement
if (num[i]<small) {
small = num[i];
}
example for numbers: 22,11,6,
So first step is num[0] < 0, why 0 as mention before small = 0 by default
Step two num[1] < 0, small stills stay 0
Step Three num[2] < 0, small stills stay 0.
What are you missing, you need to assign value to small at the first iteration of your for loop, for example:
for (int i=0; i<length; ++i) {
if(i == 0){
small = num[i];
}
if (num[i]<small) {
small = num[i];
}
if(num[i]> large) {
large = num[i];
}
}
Now your program should work
答案2
得分: 0
import java.util.Scanner;
class Average {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入元素个数:");
int length = input.nextInt();
int[] num = new int[length];
System.out.println("输入 " + length + " 个数组元素:");
int sum = 0;
int large, small;
for (int i = 0; i < length; i++) {
num[i] = input.nextInt();
sum = sum + num[i];
}
large = small = num[0]; // 小的应该在输入 num 后赋值
for (int i = 0; i < length; ++i) {
if (num[i] < small) {
small = num[i];
}
if (num[i] > large) {
large = num[i];
}
}
double avg = sum / length;
System.out.println("总和是 " + sum);
System.out.println("平均值是 " + avg);
System.out.println("最小的元素是 " + small);
System.out.println("最大的元素是 " + large);
}
}
英文:
import java.util.Scanner;
class Average {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int length = input.nextInt();
int[] num = new int[length];
System.out.println("Enter the " + length + " array elements:");
int sum = 0;
int large, small;
for (int i = 0; i < length; i++) {
num[i] = input.nextInt();
sum = sum + num[i];
}
large = small = num[0]; // small should be assigned after num is input
for (int i = 0; i < length; ++i) {
if (num[i] < small) {
small = num[i];
}
if (num[i] > large) {
large = num[i];
}
}
double avg = sum / length;
System.out.println("The sum is " + sum);
System.out.println("The average is " + avg);
System.out.println("The smallest element is " + small);
System.out.println("The largest element is " + large);
}
}
答案3
得分: 0
如果您使用Java 8或更高版本,您可以使用Arrays的流方法来简化数组的操作。[更多信息][1]
首先导入Arrays:`import java.util.Arrays;`
System.out.println("数组元素的和为:" + Arrays.stream(num).sum());
System.out.println("数组元素的平均值为:" + Arrays.stream(num).average());
System.out.println("数组中最小的元素为:" + Arrays.stream(num).min());
System.out.println("数组中最大的元素为:" + Arrays.stream(num).max());
[1]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
英文:
if you use java 8 or above, you can use the stream method of Arrays. it simplifies work with arrays. for more info
firstly import the Arrays as import java.util.Arrays;
System.out.println("The sum is " + Arrays.stream(num).sum());
System.out.println("The average is " + Arrays.stream(num).average());
System.out.println("The smallest element is " + Arrays.stream(num).min());
System.out.println("The largest element is " + Arrays.stream(num).max());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论