小编Arm*_*ure的帖子

C : getchar() 和 putchar()

所以我一直在自学 C 并且我遇到了来自“stdio.h”的“getchar()”和“putchar()”方法。据我了解,'getchar()' 从文本流中获取最新的字符并将其存储到一个变量中,而 'putchar()' 获取这个变量并将其打印到终端。

所以我写了下面的一段代码:

    #import<stdio.h>

void main () {
    printf("Enter a character and it will be repeated back to you:\n");
    int c;
    while (c != EOF) {
        c = getchar();
        printf("You entered : ");
        putchar(c);
        printf("\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它读取键盘输入并将其打印到屏幕上,一次一个字符。例如,如果我输入“home”,输出将是:

您输入了:h 您输入了:o 您输入了:m 您输入了:e

但我得到:

home 您输入了:h 您输入了:o 您输入了:m 您输入了:e

字符在输入时打印,然后重复。我不太确定我在这里做错了什么,或者我做错了什么,只是不太了解这个概念。谁能解释一下这里发生了什么?

c

2
推荐指数
1
解决办法
116
查看次数

为什么在字典<>中添加新值会覆盖其中的先前值

我一直在使用C#中的Dictionary进行此问题.每当我添加一个条目时,字典中的所有条目都填充相同的值.这是代码:

using System;
using System.Collections.Generic;

namespace TESTING
{
    class Program
    {
        /*--------------------Variable Declaration------------------------*/
        public static int NumberOfPlayers = 5;
        public static Dictionary<int, bool> PlayerAI = new Dictionary<int, bool>();
        public static Dictionary<int, Dictionary<string, int>> Players = new Dictionary<int, Dictionary<string, int>>();

        /*----------------------------------------------------------------*/
        public static void Main(string[] args)
        {
            Dictionary<string, int> TMP = new Dictionary<string, int>();
            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Console.WriteLine(i);
                TMP.Clear();
                TMP.Add("Player Target", 0 + (i * 3));
                TMP.Add("Player Cash", 0 + (i * 3));
                TMP.Add("Player …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

0
推荐指数
1
解决办法
348
查看次数

标签 统计

c ×1

c# ×1

dictionary ×1