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

时间:2015-11-17 09:47:55

标签: java arrays

所以我必须将数组中的两个元素与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] + " ");
    }
  }
}

3 个答案:

答案 0 :(得分:0)

您实际上并未交换用户输入的数字,而是始终使用4th交换1st元素。

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

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;

答案 1 :(得分:0)

你为什么不尝试这个。请记住,数组索引从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] + " ");
         }
   }
}

答案 2 :(得分:0)

你必须定义索引......

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] + " ");
            }
        }
    }