我正在爲學校製作一款遊戲,這是一款基本的老虎機,它將隨機生成數字並將數字轉換爲獨立數組中的字符。由於聲明完全被忽略,這似乎不起作用。它沒有給出正確的輸出結果,有時候它們會變成空白。案例/開關在C不工作
#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");
}
此外,你似乎已經忘了一些功能使用'y' 。另外,在'main'的開頭添加'srand(time(NULL));'。 –
@CoolGuy似乎仍然得到相同的輸出。我更新了帖子上的代碼 – Distorts