数组上返回 0 的最小值(目标是找到最大值和最小值)
提前致谢。当我打印该数组的最小值时,我无法弄清楚为什么我的代码给我带来了麻烦。它似乎保留了 min 的初始 0 值,但我不知道如何纠正这个问题。麦克斯工作得很好。我是否在程序的错误部分设置了最大值和最小值?
另外,这是我的第一个数组作业,因此我可能会对稍后打印下标有一些疑问。
再次感谢!
编辑- 据我所知,这解决了问题。谢谢丹·W.
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
System.out.print("Enter a new number. This will continue until "
+ "you reach the size of the array. ");
arrayOfNumbers[i] = keyboard.nextInt();
}
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
package program8;
import java.util.Scanner;
public class arrayPrint {
public static void main(String[] args) {
final int NUMBER_OF_ELEMENTS;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the amount of numbers the array will read in. ");
NUMBER_OF_ELEMENTS = keyboard.nextInt();
int arrayOfNumbers[] = new int[NUMBER_OF_ELEMENTS];
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
System.out.print("Enter a new number. This will continue until "
+ "you reach the size of the array. ");
arrayOfNumbers[i] = keyboard.nextInt();
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (max <= arrayOfNumbers[i])
max = arrayOfNumbers[i];
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (min >= arrayOfNumbers[i])
min = arrayOfNumbers[i];
}
System.out.print(+max+ "max and " +min+ " min");
}
}
Thanks in advance. I'm having trouble figuring out why my code is giving me trouble when I print a minimum value for this array. It seems to be retaining the initial 0 value for min, but I'm not sure how to correct this. Max works fine. Did I set the max and min values in the wrong part of the program?
Also, this is my first assignment with arrays so I might have some questions about printing subscripts later on.
Thanks again!
EDIT- this fixed the problem, to my knowledge. Thanks Dan W.
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
System.out.print("Enter a new number. This will continue until "
+ "you reach the size of the array. ");
arrayOfNumbers[i] = keyboard.nextInt();
}
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
package program8;
import java.util.Scanner;
public class arrayPrint {
public static void main(String[] args) {
final int NUMBER_OF_ELEMENTS;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the amount of numbers the array will read in. ");
NUMBER_OF_ELEMENTS = keyboard.nextInt();
int arrayOfNumbers[] = new int[NUMBER_OF_ELEMENTS];
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
System.out.print("Enter a new number. This will continue until "
+ "you reach the size of the array. ");
arrayOfNumbers[i] = keyboard.nextInt();
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (max <= arrayOfNumbers[i])
max = arrayOfNumbers[i];
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (min >= arrayOfNumbers[i])
min = arrayOfNumbers[i];
}
System.out.print(+max+ "max and " +min+ " min");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您输入什么值?因为
int
的默认值为0:这里因此,如果您只输入正数,则最小值将始终为 0。
为避免这种情况,请在用户输入数字后初始化最小值和最大值,这样最大值和最小值就不会默认为 0。
What values are you inputting? Because an
int
has a default value of 0: hereSo, if you're only inputting positive numbers, the min will always be 0.
To avoid this, initialize min and max after you have the user input the numbers, that way the max and min are not defaulted to 0.