我如何能够显示用户输入数组中的最大和最小数字?
How am I able to display the largest and the smallest number from the array of the user's input?
有人能帮我看看我的代码有什么问题吗?我在显示数组中最大和最小的部分时遇到了问题。抱歉,我是 java 的新手。非常感谢
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
}
{
if (input > max)
{
max = input;
}
else if (input <= min)
{
min = input;
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
Post 您读取了值,您需要遍历数组并尝试使用您的 mix/max 比较逻辑,例如:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int number : array) {//use separate for loop or use the same for loop to which you add numbers in array.
if (number > max) {
max = input;
}
else if (number < min) {
min = input;
}
}
使用相同的 for 循环:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] > max) {
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
您需要有此代码:
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
if (array[i]> max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
错误是
- 一对额外的大括号,以及 2. 将输入大小而不是数组元素与最小值和最大值进行比较。
可能的工作代码如下所示:-
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] >= max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
你甚至可以试试这个。
int max=array[0], min=array[0];
for(int x=0; x<array.length; x++){
max = array[x]>max?array[x]:max;
min = array[x]<min?array[x]:min;
}
首先将第一个数组元素设置为最小值和最大值。
有人能帮我看看我的代码有什么问题吗?我在显示数组中最大和最小的部分时遇到了问题。抱歉,我是 java 的新手。非常感谢
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
}
{
if (input > max)
{
max = input;
}
else if (input <= min)
{
min = input;
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
Post 您读取了值,您需要遍历数组并尝试使用您的 mix/max 比较逻辑,例如:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int number : array) {//use separate for loop or use the same for loop to which you add numbers in array.
if (number > max) {
max = input;
}
else if (number < min) {
min = input;
}
}
使用相同的 for 循环:
int min = Integer.MAX_VALUE;//change your assignment of 0 as numbers can be negative
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] > max) {
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
您需要有此代码:
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0 ; i < array.length; i++ ) {
if (array[i]> max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
错误是
- 一对额外的大括号,以及 2. 将输入大小而不是数组元素与最小值和最大值进行比较。
可能的工作代码如下所示:-
package problem6;
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args) {
int input;
int min = 0;
int max = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
input = keyboard.nextInt();
int array[] = new int[input];
for (int i = 0 ; i < array.length; i++ ) {
System.out.println("Enter number: ");
array[i] = keyboard.nextInt();
if (array[i] >= max)
{
max = array[i];
}
else if (array[i] <= min)
{
min = array[i];
}
}
System.out.print("\nLargest: " + max);
System.out.print("\nSmallest:" + min);
}
}
你甚至可以试试这个。
int max=array[0], min=array[0];
for(int x=0; x<array.length; x++){
max = array[x]>max?array[x]:max;
min = array[x]<min?array[x]:min;
}
首先将第一个数组元素设置为最小值和最大值。