2015-11-17 76 views
0

因此,我必須交換數組中的兩個元素,其中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] + " "); 
    } 
    } 
} 

回答

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

你沒有實際交換的數字,其用戶輸入,你總是交換與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

你爲什麼不試試這個。請記住,數組的下標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