2013-08-23 63 views
1

我寫一個簡單的程序,其中用戶在整數進入幾個值我已經寫到目前爲止array.The代碼是這樣在一個整數數組以獨特的輸入

#include<stdio.h> 
#include<stdlib.h> 
void main(){ 

    int array[10],x,y; 

    for(x=0;x<10;x++) 
    { 
     scanf("%d",&array[x]); 
     //if the entered value is same as any of 
     //the previously entered values 
     //prompt the user to enter again till 
     //he enters a unique value. 

     }  } 

我想在整數陣列是唯一的,如果用戶輸入先前輸入的值,則應提示他再次輸入。 我怎麼可能做到這一點? 也許使用goto語句?但我猜想這是非常令人沮喪的。 使用while循環?但我需要循環先前輸入的值來檢查重複項,我無法對其進行編碼。 任何幫助表示讚賞

+5

「我需要通過以前輸入的值循環,重複檢查,我不能夠實現代碼」 - 爲什麼不呢? –

回答

4

未經測試:

#include<stdio.h> 
#include<stdlib.h> 
void main(){ 
    int array[10],x,y; 

    x = 0; 
    while (x < 10) { 
     int duplicated = 0; 

     scanf("%d",&array[x]); 

     //if the entered value is same as any of 
     //the previously entered values 
     //prompt the user to enter again till 
     //he enters a unique value. 
     for (y = 0; y < x; y++) { 
      if (array[x] == array[y]) { 
       printf("You already entered this! Try again.\n"); 
       duplicated = 1; 
       break; 
      } 
     } 

     if (! duplicated) 
      x++; 
    }  
} 
+0

Do'h,編輯。 –

+1

應該在你使用它的時候修復'void main'? –

+0

它工作。謝謝 – fts

相關問題