如何根据用户输入将数组中的最大元素放在末尾,将最小元素放在开头?
How to put the largest element in an array at the end and the least at the start from user input?
我提取了数组中最大和最小的元素。我试图弄清楚如何将最大元素交换为数组中的最后一个元素,将最小元素交换为数组中的第一个数字,但它对我不起作用。我做错了什么?
import java.util.Scanner;
public class SmallestLargest
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in);
int[] array1 = new int [input.nextInt()];
for (int i = 0; i < array1.length; i++)
{
array1[i] = input.nextInt();
}
int max = array1[0];
int least = array1[0];
for (int i = 0; i < array1.length; i++)
{
if(max < array1[i])
{
max = array1[i];
}
else if(least > array1[i])
{
least = array1[i];
}
}
for (int i = 0; i < array1.length; i++)
{
int x = array1[0];
array1[0] = max;
array1[array1.length-1] = x;
System.out.print(array1[i] + " ");
}
input.close();
}
}
删除第二个 for 循环并执行
array1[0] = max;
array1[array1.length-1] = least;
这就是您所需要的,您必须跟踪 max/min 值所在的索引。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array1 = new int[input.nextInt()];
int maxIndex = -1;
int minIndex = -1;
for (int i = 0; i < array1.length; i++) {
array1[i] = input.nextInt();
}
int max = array1[0];
int least = array1[0];
for (int i = 0; i < array1.length; i++) {
if (max < array1[i]) {
max = array1[i];
maxIndex = i;
} else if (least > array1[i]) {
least = array1[i];
minIndex = i;
}
}
int temp = array1[0];
array1[0] = max;
array1[maxIndex] = temp;
temp = array1[array1.length - 1];
array1[array1.length - 1] = least;
array1[minIndex] = temp;
for(int a: array1){
System.out.println(a);
}
input.close();
}
}
我提取了数组中最大和最小的元素。我试图弄清楚如何将最大元素交换为数组中的最后一个元素,将最小元素交换为数组中的第一个数字,但它对我不起作用。我做错了什么?
import java.util.Scanner;
public class SmallestLargest
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in);
int[] array1 = new int [input.nextInt()];
for (int i = 0; i < array1.length; i++)
{
array1[i] = input.nextInt();
}
int max = array1[0];
int least = array1[0];
for (int i = 0; i < array1.length; i++)
{
if(max < array1[i])
{
max = array1[i];
}
else if(least > array1[i])
{
least = array1[i];
}
}
for (int i = 0; i < array1.length; i++)
{
int x = array1[0];
array1[0] = max;
array1[array1.length-1] = x;
System.out.print(array1[i] + " ");
}
input.close();
}
}
删除第二个 for 循环并执行
array1[0] = max;
array1[array1.length-1] = least;
这就是您所需要的,您必须跟踪 max/min 值所在的索引。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array1 = new int[input.nextInt()];
int maxIndex = -1;
int minIndex = -1;
for (int i = 0; i < array1.length; i++) {
array1[i] = input.nextInt();
}
int max = array1[0];
int least = array1[0];
for (int i = 0; i < array1.length; i++) {
if (max < array1[i]) {
max = array1[i];
maxIndex = i;
} else if (least > array1[i]) {
least = array1[i];
minIndex = i;
}
}
int temp = array1[0];
array1[0] = max;
array1[maxIndex] = temp;
temp = array1[array1.length - 1];
array1[array1.length - 1] = least;
array1[minIndex] = temp;
for(int a: array1){
System.out.println(a);
}
input.close();
}
}