英文:
Swapping arrays between min and max
问题
我被分配任务制作一个程序,该程序从10个随机数中找到最大和最小的数,并交换它们的位置。
我的代码:
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner obj = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("输入10个随机数:");
for(int i = 0; i < arr.length; i++) {
int num = obj.nextInt();
arr[i] = num;
}
int minIndex = findMinIndex(arr, arr.length);
int maxIndex = findMaxIndex(arr, arr.length);
// 交换最大和最小数的位置
int temp = arr[minIndex];
arr[minIndex] = arr[maxIndex];
arr[maxIndex] = temp;
System.out.print("交换后的数组: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
static int findMaxIndex(int[] numbers, int length) {
int maxIndex = 0;
for (int i = 1; i < length; i++) {
if (numbers[i] > numbers[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
static int findMinIndex(int[] numbers, int length) {
int minIndex = 0;
for (int i = 1; i < length; i++) {
if (numbers[i] < numbers[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
}
我的问题是在输出行上没有换行,例如:
输入:1 2 3 4 5 6 78 9 5 5
输出:78 2 3 4 5 6 1 9 5 5
英文:
I am tasked to make a program that finds the max and min numbers from 10 random numbers and swap their places.
My code:
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner obj = new Scanner(System.in);
int[] arr =new int[10];
System.out.println("enter 10 random numbers: ");
for(int i = 0 ; i < arr.length; i++){
int num = obj.nextInt();
arr[i] = num;
}
int min = findMin(arr, arr.length);
int max =findMax(arr, arr.length);
System.out.println("max number : " + max + " | min number : " + min);
}
static int findMax(int[] numbers, int lenght) {
int max = numbers[0];
for (int i = 1; i < lenght; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println();
return max;
}
static int findMin(int[] numbers, int lenght) {
int min = numbers[0];
for (int i = 1; i < lenght; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
return min;
}
}
My problem is i dont get print in line
example
imput 1 2 3 4 5 6 78 9 5 5
output 78 2 3 4 5 6 1 9 5 5
答案1
得分: 0
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] nums = new int[10];
System.out.println("请输入10个整数:");
for (int i = 0; i < nums.length; i++) {
nums[i] = scan.nextInt();
}
int max = nums[0];
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
if (min > nums[i]) {
min = nums[i];
} else if (max < nums[i]) {
max = nums[i];
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == min) {
System.out.print(max + ", ");
} else if (nums[i] == max) {
System.out.print(min + ", ");
} else {
System.out.print(nums[i] + ", ");
}
}
System.out.println("\n最大值为:" + max);
System.out.println("最小值为:" + min);
}
英文:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] nums = new int [10];
System.out.println("Please enter 10 ints: ");
for(int i = 0;i<nums.length;i++){
nums[i] = scan.nextInt();
}
int max = nums[0];
int min = nums[0];
for(int i = 0;i<nums.length;i++) {
if(min>nums[i]) {
min = nums[i];
}else if(max<i) {
max = nums[i];
}
}
for(int i = 0;i<nums.length;i++) {
if(nums[i]==min) {
System.out.print(max+", ");
}else if(nums[i]==max) {
System.out.print(min+", ");
}else {
System.out.print(nums[i]+", ");
}
}
System.out.println("\nmax is: "+max);
System.out.println("min is: "+min);
}
this should produce the max and min of a given random array of size 10 with out fault.
答案2
得分: 0
好的,如果您将第12行和第13行更改为(以正确查找最小值和最大值)
min = integer[0];
max = integer[0];
并在末尾添加一些代码块(以进行交换):
integer[minIndex] = max;
integer[maxIndex] = min;
那么就可以了。然后您只需打印数组。
英文:
Alright so if you change the 12th and 13th lines to (to find min and max correctly)
min = integer[0];
max = integer[0];
And add some piece of code at the end like (to swap):
integer[minIndex] = max;
integer[maxIndex] = min;
It would be alright. Then you can just print the array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论