Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c中是非提示的do while循环的使用_C - Fatal编程技术网

c中是非提示的do while循环的使用

c中是非提示的do while循环的使用,c,C,我正在学习用c在线编程,并尝试使用do while循环 这是一个骰子游戏程序 玩家可以以最低1000数量进入游戏 一个球员可以冒任意数量的险。如果骰子的值为1,则玩家将以一半的金额离开游戏 如果该值不是1,则总金额将按dice的100*值的倍数增加 如果玩家退出,他/她将带着他/她所拥有的金额离开游戏 #include <stdio.h> #include <stdlib.h> int main () { int cash, dice; char ch

我正在学习用c在线编程,并尝试使用do while循环

这是一个骰子游戏程序

玩家可以以最低
1000
数量进入游戏

一个球员可以冒任意数量的险。如果
骰子
的值为
1
,则玩家将以一半的金额离开游戏

如果该值不是
1
,则总金额将按
dice
100*值的倍数增加

如果玩家退出,他/她将带着他/她所拥有的金额离开游戏

#include <stdio.h>
#include <stdlib.h>

int main () {

    int cash, dice;
    char ch;
    printf ("cash\n");
    scanf ("%d", &cash);
    if (cash < 1000) {
        printf ("No\n");
        exit (0);
    }
    else {
        do {
            printf ("dice\n");
            scanf ("%d", &dice);
            while (dice < 1 || dice > 6) {
                printf ("\n invalid");
                printf ("dice\n");
                scanf ("%d", &dice);
            }
            if (dice == 1) {
                cash = cash / 2;
                break;
            }

            cash = (dice * 100) + cash;
            printf ("Do you want to continue");
            scanf ("%c", &ch);

        } while (ch == 'y');
    }
    printf ("won=%d", cash);

    return 0;
}
#包括
#包括
int main(){
整数现金,骰子;
char ch;
printf(“现金”);
scanf(“%d”和现金);
如果(现金<1000){
printf(“否”);
出口(0);
}
否则{
做{
printf(“dice\n”);
scanf(“%d”和骰子);
而(骰子<1 | |骰子>6){
printf(“\n无效”);
printf(“dice\n”);
scanf(“%d”和骰子);
}
如果(骰子=1){
现金=现金/2;
打破
}
现金=(骰子*100)+现金;
printf(“您想继续吗”);
scanf(“%c”和“ch”);
}而(ch='y');
}
printf(“韩元=%d”,现金);
返回0;
}
此程序不接受
y
n
输入。 它显示语句
是否要继续

然后直接转到won语句。

您必须在
%c

scanf(" %c", &ch);

更改scanf将解决所有问题

scanf(" %c",&ch); //notice space

您的第二个
scanf
正在使用
骰子
所留下的
'\n'
字符
stdin

只需在格式说明符之前添加空格,您就可以在接收用户插入的字符之前使用stdin中的所有字符:

scanf (" %c", &ch);
scanf()

printf ("dice\n");
scanf ("%d", &dice);
这在缓冲区中留下了“\n”,您对scanf的第二次调用将读取此“\n”而不是“n”,并继续。在读取字符之前,必须先读取剩余的“\n”

man scanf(3):

有两种选择

scanf (" %c", &ch);


您遇到的最大问题是没有考虑到输入缓冲区(
stdin
)中留下的
'\n'
,这是由于用户在输入
dice
后按Enter键导致的,因为
%c
格式说明符将愉快地将
'\n'
作为
ch
的用户输入

下一个问题是
ch
应该是和
int
不是
char
,否则您将永远无法测试/trap
EOF

此外,您无法验证
scanf
返回值以确认任何转换是否有效

考虑到与使用
scanf
获取用户输入相关的问题,您最好使用
fgets
获取用户输入,并将整行用户输入读取到足够大小的缓冲区中,然后使用
sscanf
(或者简单地减去一位数字的
'0'

将这些部分放在一起,您可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 128

int main () {

    int ch, cash, dice;
    char buf[BUFLEN] = "";
    printf ("cash: ");

    if (!fgets (buf, BUFLEN, stdin)) {  /* read/validate cash */
        fprintf (stderr, "error: invalid input - cash.\n");
        return 1;
    }
    if (sscanf (buf, "%d", &cash) != 1) { /* parse cash from buf */
        fprintf (stderr, "error: invalid conversion - cash.\n");
        return 1;
    }

    if (cash < 1000) {
        printf ("No\n");
        return 1;
    }

    do {
        ch = 'n';  /* set/reset ch to exit each iteration */

        printf ("dice: ");
        if (!fgets (buf, BUFLEN, stdin)) { /* read/validate dice */
            fprintf (stderr, "error: invalid input - dice.\n");
            break;  /* break on EOF */
        }
        /* parse/validate  dice */
        if (sscanf (buf, "%d", &dice) != 1 || dice < 1 || dice > 6) {
            fprintf (stderr, "error: invalid conversion - dice.\n");
            ch = 'y';   /* set ch to 'y' */
            continue;   /* prompt again, etc */
        }

        if (dice == 1) {
            cash = cash / 2;  /* note: truncated division */
            break;
        }
        cash = (dice * 100) + cash;
        printf ("\nDo you want to continue? (y/n): ");
        if (fgets (buf, BUFLEN, stdin))
            ch = *buf;  /* simply assign first char */
        else
            break;  /* exit loop on EOF */

    } while (ch == 'y');

    printf ("\nwon = %d\n\n", cash);

    return 0;
}

查看所有答案,然后决定是否使用
scanf
fgets/sscanf
。这两种方法都是可行的,当您在一个
scanf
电话中同时进行读取和解析时,您只会失去一点灵活性。如果您有任何问题,请告诉我。

非常感谢@blackbird,尽管这段代码可能有助于解决问题问题,它没有解释为什么和/或如何回答问题。提供此附加上下文将显著提高其长期价值。请您的答案添加解释,包括适用的限制和假设。虽然此代码可能有助于解决问题,但它没有解释为什么和/或如何回答问题补充说明。提供这种额外的背景将显著提高其长期价值。请您的回答补充说明,包括适用的限制和假设。
(void) getchar ();
scanf ("%c", &ch);
#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 128

int main () {

    int ch, cash, dice;
    char buf[BUFLEN] = "";
    printf ("cash: ");

    if (!fgets (buf, BUFLEN, stdin)) {  /* read/validate cash */
        fprintf (stderr, "error: invalid input - cash.\n");
        return 1;
    }
    if (sscanf (buf, "%d", &cash) != 1) { /* parse cash from buf */
        fprintf (stderr, "error: invalid conversion - cash.\n");
        return 1;
    }

    if (cash < 1000) {
        printf ("No\n");
        return 1;
    }

    do {
        ch = 'n';  /* set/reset ch to exit each iteration */

        printf ("dice: ");
        if (!fgets (buf, BUFLEN, stdin)) { /* read/validate dice */
            fprintf (stderr, "error: invalid input - dice.\n");
            break;  /* break on EOF */
        }
        /* parse/validate  dice */
        if (sscanf (buf, "%d", &dice) != 1 || dice < 1 || dice > 6) {
            fprintf (stderr, "error: invalid conversion - dice.\n");
            ch = 'y';   /* set ch to 'y' */
            continue;   /* prompt again, etc */
        }

        if (dice == 1) {
            cash = cash / 2;  /* note: truncated division */
            break;
        }
        cash = (dice * 100) + cash;
        printf ("\nDo you want to continue? (y/n): ");
        if (fgets (buf, BUFLEN, stdin))
            ch = *buf;  /* simply assign first char */
        else
            break;  /* exit loop on EOF */

    } while (ch == 'y');

    printf ("\nwon = %d\n\n", cash);

    return 0;
}
$ ./bin/rfmt
cash: 1200
dice: 7
invalid dice.
dice: 5

Do you want to continue? (y/n): y
dice: 2

Do you want to continue? (y/n): y
dice: 6

Do you want to continue? (y/n): n

won = 2500