2016-04-29 109 views
0

我正在爲學校製作一款遊戲,這是一款基本的老虎機,它將隨機生成數字並將數字轉換爲獨立數組中的字符。由於聲明完全被忽略,這似乎不起作用。它沒有給出正確的輸出結果,有時候它們會變成空白。案例/開關在C不工作

輸出:http://distorts.me/imgs/files/132ca7cc1da6fc459de3e498d8e12b9f.png

#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 
/* 
George Mason 
Slots 
Date Started: 4/25/16 
Date Finished: 
Dr K. 
*/ 
void loadScreen(); 
void spacer(); 
void tabSpacer(); 
void clearScr(); 
int printMachine(char*, int); 
int randomNum(int*, int); 
int convertNum(int*, char*, int); 
int rand_int(); 
void game(); 
int main(){ 
    srand(time(NULL)); 
    int stop; 
    loadScreen(); 
    clearScr(); 
    game(); 
    scanf("%d", &stop); 
} 
void game(){ 
    int tokens = 5, randomNums[9], x, y = 9; 
    char randomChars[9], userInput; 
    randomNum(randomNums, 9); 
    convertNum(randomNums, randomChars, 9); 
    printMachine(randomChars, 9); 

} 
int printMachine(char* randomChars, int y){ 
    printf("-------------\n"); 
    printf("| %c | %c | %c | \n", randomChars[0], randomChars[1], randomChars[2]); 
    printf("| %c | %c | %c | \n", randomChars[3], randomChars[4], randomChars[5]); 
    printf("| %c | %c | %c | \n", randomChars[6], randomChars[7], randomChars[8]); 
    printf("-------------"); 
} 
int randomNum(int* randomNums, int y){ 
    int x,a = 0, b = 9; 
    for(x = 0; x < y; x++){ 
     randomNums[x] = ((rand() % (b-a+1)) + a); 
    } 
} 
int convertNum(int* randomNums, char* randomChars, int y){ 
    int x; 
    for(x = 0; x < 9; x++){ 
      switch(randomNums[x]){ 
       case 1: 
        randomChars[x] = '@'; 
        break; 
       case 2: 
        randomChars[x] = '#'; 
        break; 
       case 3: 
        randomChars[x] = '$'; 
        break; 
       case 4: 
        randomChars[x] = '+'; 
        break; 
       case 5: 
        randomChars[x] = '&'; 
        break; 
       case 6: 
        randomChars[x] = '*'; 
        break; 
       case 7: 
        randomChars[x] = '?'; 
        break; 
       case 8: 
        randomChars[x] = '!'; 
        break; 
       case 9: 
        randomChars[x] = '~'; 
        break; 
       default: 
        randomChars[x] = 'e'; 
        break; 
      } 
    } 
} 
void loadScreen(){ 
    int x; 
    for(x = 0; x < 3; x++){ 
     spacer(); 
    } 
    tabSpacer(); 
    printf("Please wait 5 seconds while we load the saved data.\n"); 
    tabSpacer(); 
    printf(" If there is no saved data one will be created."); 
    sleep(5); 
} 
void spacer(){ 
    int x; 
    for(x = 0; x < 3; x++){ 
      printf("\n"); 
    } 
} 
void tabSpacer(){ 
    printf("\t "); 
} 
void clearScr(){ 
    system("cls"); 
} 
+0

此外,你似乎已經忘了一些功能使用'y' 。另外,在'main'的開頭添加'srand(time(NULL));'。 –

+0

@CoolGuy似乎仍然得到相同的輸出。我更新了帖子上的代碼 – Distorts

回答

5

你已經忘了保持break;所有情況後,所以它即運行所有案件從案件1箱9
在年底會節省randomChars[x] = '~';(殼體9) 。

case 1: 
    randomChars[x] = '@'; 
    break; 
case 2: 
    randomChars[x] = '#'; 
    break; 
    . 
    . 
    . 
    . 

case 7: 
    randomChars[x] = '?'; 
    break; 
case 8: 
    randomChars[x] = '!'; 
    break; 
case 9: 
    randomChars[x] = '~'; 
    break; 

編輯:

  • 爲什麼你的函數返回類型爲int當你不返回任何值。將int更改爲void。前main()

  • 我得到的輸出喜歡這個

  • 功能game()未聲明:
    enter image description here

+0

我的輸出看起來還是一樣的 – Distorts

+0

^^^^^^請閱讀 – Distorts

+0

請閱讀我無法弄清楚 – Distorts