简体   繁体   中英

Trying to swap array elements based on user input

So I have to swap two elements in an array with 5 values but the two to swap have to be taken from the keyboard. The previous question was to swap to specific ones and I got it done but I'm not sure how to get the numbers from the keyboard to be used in the swap. Some of this is left from the previous one where I knew the elements I had to swap.

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

You're not actually swapping the numbers which user enters, you're always swapping 4th element with the 1st .

To swap the elements you need to find out the index where they belong.

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

Now to swap those elements, call this function.

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;

Why don't you try this. Please keep in mind that array indices start at 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] + " ");
         }
   }
}

you have to define index...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM