【发布时间】:2014-11-30 00:51:03
【问题描述】:
所以我在使用 C 的代码时遇到了问题。 还没有学会如何使用 do/while 命令。我永远看不到让决赛是正确的。 我尝试编码以显示随机数(例如 9) 我按 9 说太低了。然后最后随机数说是14。 我不确定如何在用户的输入尝试中获得一致的随机数。
// This is a guessing game. You have 4 attempts to guess the number between 1-20.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int usersInput;
int range;
range = rand() %21;
printf("Hello and welcome to my game! I have a number in my head between 1-20.\n");
printf("Try and guess what it is.\n");
printf("Please enter your guess as an integer.\n");
scanf("%d", &usersInput);
// These are the cases for your first attempted
// Attempt 1
if (usersInput > range) {
printf("Your guess is too high. Please try again\n");
}
else if (usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if (usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 2
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. Please try again\n");
}
else if(usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 3
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. Please try again\n");
}
else if(usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 4
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. You lose!\n");
printf("the random number is %d \n", rand() %21);
}
else if(usersInput < range){
printf("Your guess is too low. You Lose!\n");
printf("the random number is %d \n", rand() %21);
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
printf("the random number is %d \n", rand() %21);
return 0;
}
}
【问题讨论】:
-
顺便说一句,用户名的选择不错:)
标签: c if-statement random