[英]rand() keeps giving me the same number and decreasing 4 by variable
我試圖在我上過的課上做這項工作,但由於某種原因我無法讓rand()
工作,我已經測試了好幾種不同的方法來嘗試使其工作,但我只是想不通出來。 我是C的新手,所以我在這里做錯了,請幫忙。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
srand( time(NULL) );
int num, rep, nu, ba, bas;
char B[20] = "";
char C[20] = "";
printf("Jogo: Converter Bases!\n");
printf("\n Quantas perguntas desejas responder? (1-6)");
scanf("%d", &num);
while (num <= 0 || num > 6) {
printf("\n Introduz um valor valido de 1 a 6: ");
scanf("%d", &num);
}
for (int p=0; p<num; p++) {
nu=rand()% 10;
bas=rand()% 3;
ba=rand()% 3;
printf("\nnu = %i bas = %i ba = %i", &nu, &bas, &ba);
while (ba = bas) {
ba = rand()% 3;
}
if (bas = 0) {
char B[20] = "Octal";
} else if (bas =1) {
char B[20] = "Decimal";
} else {
char B[20] = "Hexadecimal";
}
if (ba = 0) {
char C[20] = "Octal";
} else if (ba =1) {
char C[20] = "Decimal";
} else {
char C[20] = "Hexadecimal";
}
printf("Converte %i de base %c para base %c\n", &nu, &B, &C);
scanf("%d", &rep);
}
return 0;
}
`
這是因為您正在寫:
printf("%d", &x);
什么時候應該寫作
printf("%d", x);
在第一種情況下,您要打印變量的內存地址 ; 在第二個-變量的內容。
scanf
需要&
,因為它需要知道將結果放在哪里的內存地址,但是printf只需要該值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.