【问题标题】:C Programming: A Simple Countdown ProgramC 编程:一个简单的倒计时程序
【发布时间】:2016-08-04 22:04:56
【问题描述】:

我正在尝试创建一个与倒计时电视节目相关的简单程序。我才开始,所以我的问题是从一开始。当我要求用户输入 c 或 v 作为辅音或元音时,我只是在检查它是否返回是、否或请再试一次。这有效,但它直接重复自身,并返回请重试。我觉得这是一个简单的问题,但我不知道它是什么。

#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;
}

【问题讨论】:

  • 修复代码缩进。
  • C 不是 Python,你不能返回元组。 (count, letter 将只返回 letter。)
  • 不要包含您要告诉我们忽略的任何内容。如果它无关紧要,请从你的问题中删除它。
  • 当您使用scanf_s 时,请确保为所有%s%c%[ 格式指定最大缓冲区计数:scanf_s("%c", &amp;letter, 1);

标签: c if-statement while-loop char scanf


【解决方案1】:

在我看来,您没有考虑输入中的换行符,并且它也通过您的子例程发送。添加一个 else if 子句来测试\n,并进行相应的处理。

上面的 C 存在多个其他问题,但据我所知,这是与问题相关的问题。

【讨论】:

    【解决方案2】:

    NUIGProbz

    你基本上需要创建一个9个字母的数组,初始化为NULL, 创建 v 和 c 的 arr,让用户选择他们的字母,直到 9 个字母数组已满,打印出字母,让用户输入一个单词,交叉引用字典,如果该单词存在,则打印出他们的分数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2018-09-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多