c - 输出前双输入
问题描述
我遇到了一些代码的问题,我的输入提示都在输出之前运行,而结构在这里 -
scanf("%d\n\n", &d);
printf("\nYour result is: %d. \nWhat is the random number?:", r * d);
scanf("%d\n\n", &s);
所以发生的是我得到第一个输入提示,然后是第二个,然后 printf 运行。我将如何修复它以便 scanf 运行,然后 printf,然后再次 scanf?
完整的帮助代码 -
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int d;
int s;
int main() {
//r for rand, d for dividing number, s for sum
int r = rand() % 1 + 10;
time_t SFE = time(NULL);
srand(SFE);
r = rand() % r;
printf("Please choose a number: ");
scanf("%d\n\n", &d);
printf("\nYour result is: %d. \nWhat is the random number?:", r * d);
scanf("%d", &s);
if (s == r) {
printf("\nYou have the correct number!");
}
else {
printf("\nSorry, that's wrong. The correct answer is %d! Try again next time!", r);
}
return 0;
}
解决方案
谢谢用户3121023
答案只是删除换行符。
推荐阅读
- reactjs - 在反应中呈现搜索结果
- matlab - (n,n,m)矩阵的不同3d图:MATLAB
- python - 用于在一系列数字上检查勒让德猜想的 Python 代码
- arrays - 如何生成包含在numpy数组中的随机字符串对
- twitter4j - Twitter4J 搜索 API 是否支持 Twitter Premium 或 Enterprise API 搜索端点
- html - 拖放后如何使用可拖动和可调整大小?
- javascript - Bootstrap datetimepicker 中的周视图
- javascript - vue.js 重定向到另一个页面(与原始页面相同的代码)然后数据库访问 404 错误
- python - 如何将 RSTP 与 OpenCV 连接?
- python - 如何使用python检测数组对