作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个与倒计时电视节目相关的简单程序。我才刚刚开始,所以我的问题从一开始就存在。当我要求用户输入 c 或 v 作为辅音或元音时,我只是检查它是否返回 yes、no 或请重试。这有效,但它直接重复自身,返回请重试。我觉得这是一个简单的问题,但我不知道它是什么。
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int test(int count, char lett);
int main()
{
int i, counter;
char letter;
//char *fileName = "webster.txt";
printf("Welcome to Countdown!\n\n");
printf("The objective of the game is to produce the largest word from the nine letter(consonants and/or vowels) that are chosen at random by you. ");
printf("The computer will then find the longest word available from these letters to compare with your word.\n");
printf("Let's begin!\n");
counter = 0;
while (counter < 9) {
printf("\nWould you like a consonant or a vowel(Enter either c or v)? ");
scanf_s("%c", &letter);
test(counter, letter);
}
return 0;
}
int test(int count, char letter)
{
if (letter == 'c') {
printf("yes\n");
count++;
}
else if (letter == 'v') {
printf("no\n");
count++;
}
else {
printf("Please try again\n");
}
return count, letter;
}
最佳答案
在我看来,您没有考虑输入中的换行符,并且它也通过您的子例程发送。添加 else if 子句来测试 \n
,并进行相应处理。
上面的 C 还有多个其他问题,但据我所知,这是与该问题相关的问题。
关于C编程: A Simple Countdown Program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604684/
我是一名优秀的程序员,十分优秀!