简体   繁体   English

为什么在if语句前后加“\n”就不会加空行?

[英]Why will a blank line not be added if I add a "\n" before and after if statements?

I attached some code below for my final output, I would like to have a full space between the entered values after -1 and the listing of the even and odd numbers.我在下面为我的最终 output 附上了一些代码,我希望在 -1 之后的输入值与偶数和奇数列表之间有一个完整的空格。 Thus far I have put printf("\n");到目前为止,我已经把printf("\n"); after the final if statement and before when they do not equal -1 .在最终的if语句之后和它们不等于-1之前。 Any help would be appreciated任何帮助,将不胜感激

#include <stdio.h>

int main() {
    int input, numberEven, numberOdd;
    numberEven = 0;
    numberOdd = 0;
        
    do 
    {
        printf("Enter a value:");
        scanf("%d", &input);

        if (input != -1 && input % 2 == 0)
        {
            numberEven += 1;  
        }

        if (input != -1 && input % 2 != 0) 
        {
            numberOdd += 1;
        }
    }
    while (input != -1);
        
    printf("\nThe number of odd numbers is %d \n", numberOdd);
    printf("The number of even numbers is %d \n", numberEven);
    return 0;
}

Whether an empty line appears before The number of odd numbers is depends on the terminal:前面是否出现空行The number of odd numbers is取决于终端:

  • on linux and OS/X, reading from the terminal in cooked mode causes the terminal to echo user input after the prompt Enter a value: including the newline entered by the user, then the program outputs the string "\nThe number of odd numbers is...\n" starting with a newline, hence producing an empty line.在 linux 和 OS/X 上,在 cooked 模式下从终端读取会导致终端在提示Enter a value:包括用户输入的换行符后回显用户输入,然后程序输出字符串"\nThe number of odd numbers is...\n"以换行符开始,因此产生一个空行。
  • on some legacy systems, such as Windows, user input is echoed but the newline seems missing and the next program output appears on the same line, but since you start with a newline, The number... appears on the next line without an intervening empty line.在某些遗留系统上,例如 Windows,用户输入被回显,但换行符似乎丢失,下一个程序 output 出现在同一行,但由于您从换行符开始, The number...出现在下一行而没有干预空行。

The final newline in printf("The number of even numbers is %d \n", numberEven) causes the program output to end with a newline, so the command prompt appears on the next line on unix systems, but the behavior might be different on other systems. printf("The number of even numbers is %d \n", numberEven)中的最后一个换行符导致程序 output 以换行符结尾,因此在 unix 系统上命令提示符出现在下一行,但行为可能不同在其他系统上。 If you want a blank line after this output, add an extra \n at the end of the format line.如果你想在这个 output 之后有一个空行,在格式行的末尾添加一个额外的\n

It is unclear what you mean by I have put printf("\n");不清楚你的意思是我把printf("\n"); after the final if statement and before when they do not equal -1 .在最终的if语句之后和它们不等于-1之前 You should add an extra \n at the beginning of the format string if required.如果需要,您应该在格式字符串的开头添加一个额外的\n

Note that you should test the return value of scanf() to detect invalid input and premature end of file.请注意,您应该测试scanf()的返回值以检测无效输入和文件过早结束。

Note also that if the user inputs multiple values at the prompt, the behavior will be somewhat surprising but consistent as no further user input from the terminal is needed for the subsequent prompts:另请注意,如果用户在提示时输入多个值,行为会有些令人惊讶但一致,因为后续提示不需要来自终端的进一步用户输入:

chqrlie@linux:~/dev/stackoverflow$ gcc 220305-evenodd.c && ./a.out
Enter a value:1 2 3 4 5 -1
Enter a value:Enter a value:Enter a value:Enter a value:Enter a value:
The number of odd numbers is 3
The number of even numbers is 2
chqrlie@linux:~/dev/stackoverflow$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM