【问题标题】:If statement in a do while loop being ignoreddo while 循环中的 If 语句被忽略
【发布时间】:2023-02-15 06:54:23
【问题描述】:

我正在尝试制作一个程序来检测用户输入并根据该输入打印内容。输入需要设置限制,但是我设置的 if 语句被忽略了,我不太清楚为什么

#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
#include <stdio.h>
#include <conio.h>
#include <Windows.h>

int main(void) 
{
    int num;
    int exit; 

    exit = 0;

  printf("\nLab5 p1 by Chung - En Hou\n");

    num = 0;
    do{
        printf("\nEnter a number between 0 and 255:");
        //input
        scanf("%d", &num);
        //ignored if statement?
        if (num < 256 || num >= 0) {

            Sleep(250);
            printf("%d\n", num);
            Sleep(250);
            printf("%x\n", num);
            Sleep(250);
            printf("%c\n", num);

            printf("Press any button to Run the program again or press Esc to exit");

            exit = getch();
        }
        //else also ignored
        else {
            printf("\nthat number is turbo cringe please try again\n");
            printf("\nPress any button to Run the program again or press Esc to exit\n");
            exit = getch();
        }
    } while (exit != 27);


      return 0;
}

【问题讨论】:

  • “忽略”到底是什么意思?它总是转到else块?尽管不满足条件,它总是执行 if 块?
  • 所有数字要么大于或等于 0,要么小于 256——或两者兼而有之。您可能需要 &amp;&amp; 符合您的条件。

标签: c


【解决方案1】:

我想你的意思是

    if (num < 256 && num >= 0) {

代替

    if (num < 256 || num >= 0) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2022-01-26
    • 2020-05-07
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多