2016-12-04 107 views
-2

我需要做的是要求用戶輸入兩個數組的值,然後分別輸出它們,並輸出一個按升序排列的合併數組。合併/排序我的數組(Java)

例如,如果用戶爲第一個數組輸入2,5,8,0和爲第二個數組輸入6,7,0,則合併數組應該輸出2,5,6,7,8。

我的第一個兩個數組的輸出工作正常,但合併數組總是輸出一個零。我還添加了一個重新啓動布爾值來查看用戶是否想再次嘗試。當我被卡住時請幫助我。

我瞭解雖然過程,但不知道如何實現這個到我的代碼。這裏是我的代碼:

//import packages 
import java.util.Scanner; 
import java.lang.Math; 
import java.util.Arrays; 

public class Main4{ 

public static void main(String[] args){ 

boolean doItAgain = true;//add boolean value to use when restarting progam 

Scanner scan = new Scanner (System.in);//initialize new scanner 

while(doItAgain){ 

//initialize variables 
int first [] = new int[10000];//initialize to maximum of 10,000 integers 
int second [] = new int[10000];//initialize to maximum of 10,000 integers 
int input1; 
int input2; 
int counter1 = 0;//counter variable for first string 
int counter2 = 0;//counter variable for second string 


System.out.println(""); 
System.out.println("Welcome To my Merge Array Program 2.0!"); 
System.out.println("Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit"); //asks user for first array input 

//loop to go through each index 
for (int a = 0; a<10000; a++) 
{ 
    input1 = scan.nextInt();//stores input as input1 
    first [a] = input1; 
    counter1++; 
    if (input1<=0) 
    break;//breaks out of loop if input1 value is 0 or below 
} 

int first2 []= new int [counter1-1]; 
for(int b = 0; b<first2.length; b++) { 
    first2 [b] = first[b]; 
} 


System.out.println("Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit"); //asks user for second array input 

for (int j = 0; j<10000; j++) 
{ 
    input2 = scan.nextInt();//stores input as input2 
    second [j] = input2; 
    counter2++; 
    if (input2<=0) 
    break;//breaks out of loop if input1 value is 0 or below 
} 

int second2 []= new int [counter2-1]; 
for(int c = 0; c<second2.length; c++) { 
    second2 [c] = second[c]; 
} 

System.out.println("First Array:");//output first array values in the order of their input 
for (int p=0; p<first2.length; p++) { 
    System.out.print(first2[p] + " "); 
} 
    System.out.println("\nSecond Array:");//output second array values in the order of their input 
    for (int p2=0; p2<second2.length;p2++) { 
    System.out.print(second2[p2] + " "); 
    } 

    boolean valid = true; 
    for (int e = 0; e<first2.length-1; e++) { 
    if(first2[e]>first2[e+1]) { 
     valid = false; 
    } 

    } 
    for (int e2 = 0; e2<second2.length-1;e2++) { 
    if(second2[e2]>second2[e2+1]) { 
     valid = false; 
    } 
    } 


    int[] array = new int[first2.length + second2.length]; 

    //fill array 3 with arrays 1 & 2 
    for(int k = 0; k <first2.length;k++){ 
    array[k] = first2[k]; 
    } 

    for (int l = 0; l<second2.length; l++){ 
    array[first2.length + l] = second2[l]; 
    } 

    //sort array 3 
    for (int i = 0; i<first2.length + 1; i++){ 
    for (int j = i+1; j<first2.length + 1; j++){ 

     if(array[i] > array[j]){ 
     int temp = array[i]; 
     array[i] = array[j]; 
     array[j] = temp; 
     } 
    } 
    } 

    //output sorted merged array 
    System.out.println("\nMerged Array: "); 
    for(int p3 = 0; p3<array.length; p3++) { 
    System.out.print(array[p3] + " "); 
    } 

    //Asks user if they want to restart program. Used boolean value to initialize doItAgain variable 
    System.out.println(""); 
    System.out.println(""); 
    System.out.println("Thanks for using this program! Do you want to do it again? (Y or N)"); 

    if(scan.next().toLowerCase().equals("y")){ 

    doItAgain = true; 
    break; 
    } 

    else{ 

    doItAgain = false; 
    System.out.println("If you change your mind and want to run it again, type runMain.");//output closing statement if user says N to restart 
    break; 
    } 
} 
} 
} 
+2

請不要編輯您的問題。對於那些稍後可能會偶然發現的人來說沒有任何意義。 – Makoto

回答

1

你沒有合併你的arrays。例如,你的第一個array是[1 2],而你的第二個array是例如[3 4]。你的最終陣列將被初始化爲4(first2.length + second2.length),但它的所有元素將爲零。

在這條線在這裏,我建議你使用arraycopy()填寫您的最終array

int[] array = new int[first2.length + second2.length]; 

System.arraycopy(first2, 0, array, 0, first2.length); 
System.arraycopy(second2, 0, array, first2.length, second2.length); 

這將first2陣列複製到您的最終數組的起始位置,然後你second2陣列複製到位置你的最終陣列在哪裏first2結束。您將結束與[1 2 3 4],然後可以將元素進行排序(儘管在這種情況下,他們已經整理

欲瞭解更多有關arraycopy()諮詢本頁面在這裏: https://www.tutorialspoint.com/java/lang/system_arraycopy.htm

編輯: BTW,你有一個錯誤就在這裏,這是什麼阻止您打印完整的有序數組:

//output sorted merged array 
System.out.println("\nMerged Array: "); 
for(int p3 = 0; p3<array.length; p3++) { 
    System.out.print(array[p3] + " "); 
} //right here, you need to close this curly bracket 

EDIT2:既然你不能使用arraycopy(),你可以ü se for循環以填寫最終陣列:

for(int k = 0; k <first2.length;k++){ 
    array[k] = first2[k]; 
} 

for (int l = 0; l<second2.length;l++){ 
    array[first2.length + l] = second2[l]; 
} 
+0

@JohnHughes只需使用'for'循環將最後一個數組的'0'替換爲其他兩個數組中的元素('first2'和'second2')即可。 –

+0

@JohnHughes 4個零在技術上是正確的,因爲那是你的最後一個數組。使用我提到的'for'循環,它可以正常工作。 「是的重新啓動」,你將不得不解決。 –

+0

在上面的編輯中,我會在哪放? –