尝试根据用户输入交换数组元素

问题描述:

因此,我必须交换数组中的两个元素,其中5个值,但交换的两个元素必须从键盘中取出。前面的问题是交换到特定的,我完成了,但我不知道如何从键盘上的数字被用于交换。其中一些是从前一个我知道我不得不交换的元素。尝试根据用户输入交换数组元素

import java.util.Scanner; 
public class Swap2 { 

    public static void main(String []args) { 
    Scanner keyboardIn = new Scanner (System.in); 

    int[] numbers = new int []{12,9,33,28,5}; 
    int temp = 0, first, second; 

    System.out.println ("Before the swap: "); 
    for(int i=0; i < numbers.length; i++) { 
     System.out.print(numbers[i] + " "); 
    } 
    System.out.println(); 
    System.out.println ("Enter the first number to swap:"); 
    first = keyboardIn.nextInt(); 
    System.out.println ("Enter the second number to swap:"); 
    second = keyboardIn.nextInt(); 
    System.out.println(); 
    temp = numbers[3]; 
    numbers [3] = numbers [1]; 
    numbers [1] = temp; 

    System.out.println ("After the swap:"); 
    for (int i = 0; i < numbers.length; i++) { 
     System.out.print (numbers[i] + " "); 
    } 
    } 
} 

你必须定义指数开始......

import java.util.Scanner; 

    public class Swap { 
     public static void main(String[] args) { 
      Scanner keyboardIn = new Scanner(System.in); 

      /* enter code here */int[] numbers = new int[] { 12, 9, 33, 28, 5 }; 
      int temp = 0, first, second; 
      int index1 = 0 , index2 = 0; 
      int j , k ; 

      System.out.println("Before the swap: "); 
      for (int i = 0; i < numbers.length; i++) { 
       System.out.print(numbers[i] + " "); 

      } 
      System.out.println(); 
      System.out.println("Enter the first number to swap:"); 
      first = keyboardIn.nextInt(); 
      System.out.println("Enter the second number to swap:"); 
      second = keyboardIn.nextInt(); 
      System.out.println(); 
      for(k = 0 ; k < numbers.length; k++){ 
       if(numbers[k] == first) 
        index1 = k; 
      } 
      for(j = 0 ; j < numbers.length; j++){ 
       if(numbers[j] == second) 
        index2 = j; 
      } 
      temp = numbers[index1]; 
      numbers[index1] = numbers[index2]; 
      numbers[index2] = temp; 

      System.out.println("After the swap:"); 
      for (int i = 0; i < numbers.length; i++) { 
       System.out.print(numbers[i] + " "); 
      } 
     } 
    } 

你没有实际交换的数字,其用户输入,你总是交换与1st4th元素。

要交换元素,您需要找出它们所属的索引。

public static int indexOf(int []arr, int ele) 
{ 
    for(int i = 0; i < arr.length; ++i) 
    if(arr[i] == ele) 
    return i; 
    return -1; 
} 

我们交换这些元素,调用这个函数。

int first = sc.nextInt(); 

int firstIndex = indexOf(arr, first); 

int second = sc.nextInt(); 

int secondIndex = indexOf(arr, second); 

int temp; 

temp = arr[firstIndex]; 
arr[firstIndex] = arr[secondIndex]; 
arr[secondIndex] = temp; 
+0

是啊,这一点从前面的问题这是交换第1个要素与第4,而遗留下来的我试图找出如何让用户输入元素交换。 – Ant695

+0

@ Ant695所以你就是这么做的:P –

+0

谢谢,这真的很有帮助。 – Ant695

你为什么不试试这个。请记住,数组的下标0

import java.util.Scanner; 




    public class Swap2 
    { 
    public static void main(String []args) 
    { 
    Scanner keyboardIn = new Scanner (System.in); 

    int[] numbers = new int []{12,9,33,28,5}; 
    int temp, first, second; 

     System.out.println ("Before the swap: "); 
     for(int i=0; i < numbers.length; i++) 
     { 
     System.out.print(numbers[i] + " "); 

     } 
      System.out.println(); 
      System.out.println ("Enter the first number to swap:"); 
      first = keyboardIn.nextInt(); 
      System.out.println ("Enter the second number to swap:"); 
      second = keyboardIn.nextInt(); 
      System.out.println(); 
      temp = numbers[first]; 
      numbers [first] = numbers [second]; 
      numbers [second] = temp; 

     System.out.println ("After the swap:"); 
     for (int i = 0; i < numbers.length; i++) 
     { 
      System.out.print (numbers[i] + " "); 
     } 
    } 
} 
+0

感谢您的帮助。 – Ant695