从数组输入几个数字和整数或不是每一个号检查 [英] Input several numbers from array and each one number check for integer or not

查看:108
本文介绍了从数组输入几个数字和整数或不是每一个号检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我希望有人可以帮我找出C语言的东西。
这是我第一次认真的功课在里面,我没有经验,我在电子学研究,所以老师的帮助也不是很用。

我需要开发的C语言控制台应用程序。用户需要输入10整数,如果插入的数目不是整数,需要输出错误并再次重新输入新的号码,直到所有10整数将被插入。

一切正常的情况下,如果我说这10个号码不能为0(我做这是确保我的if-else语句的工作),但我想,每一个输入的数字将支票将​​无法正常工作如果它是整数或不

我怎么能这样做的权利。

请帮忙

到目前为止,我的code是这样的:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
    INT I;
    浮F;
    INT数字[10];    对于(I = 0; I&小于10;我+ +)
    {
        scanf函数(%d个,&安培;数字[I]);
        如果(数字[I]!= 0)
        {
            scanf函数(*%D,&安培;数字[I]);
        }
        其他
        {
            的printf(!\\ n错误输入的号码岂不等于整数\\ n);
            的printf(\\ n请插入数了一遍的\\ n);
            scanf函数(%* D,&安培;数字[I]);
        }    }}


解决方案

 的#include<&stdio.h中GT;诠释主要(无效){
    INT I = 0;
    INT VAL;
    焦炭CH;
    INT数字[10];    而(ⅰ小于10){
        VAL = scanf函数(%d个数字+ I); //读取整数为[I]
        如果(VAL!= 1){
            而((CH =的getchar())!='\\ n')//丢弃无效的输入
                ; //空声明
            的printf(!错误输入的号码不是整数\\ n);
            的printf(请重新输入一个整数\\ n);
            VAL = scanf函数(%d个数字+ I);
            继续;
        }
        ++我;
    }
    //过程的数字阵列
    返回0;
}

我再次写这行

  VAL = scanf函数(%d个数字+ I);

现在它的工作原理我怎么需要。伟大的 - 非常感谢

everyone! I hope someone can help me figure out something in C language. This is my first seriously homework in IT, I have no experience and I'm learning in e-studies, so teacher help isn't very available.

I need to develop console application in C language. User need to input 10 integer numbers, if insert number isn't integer, need to output error and again re-enter new number until all 10 integer numbers will be inserted.

Everything works in case if I say that these 10 numbers can't be 0 (I make this to be sure that my if-else statement working), but won't work when I want that every input number will be check if it is integer or not.

How can I do it right.

Please help

so far my code look like this:

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

int main()
{
    int i;
    float f;
    int numbers[10];

    for (i = 0; i < 10; i++)
    {
        scanf ("%d", &numbers[i]);
        if (numbers[i] != 0)
        {
            scanf ("*%d", &numbers[i]);
        }
        else
        {
            printf ("\nError!Entered number is't integer \n");
            printf ("\nPlease insert number again \n");
            scanf("%*d", &numbers[i]);
        }

    }

}

解决方案

#include <stdio.h>

int main(void) {
    int i = 0;
    int val;
    char ch;
    int numbers[10];

    while(i < 10) {
        val = scanf("%d", numbers + i);  // read the integer into a[i]
        if(val != 1) {
            while((ch = getchar()) != '\n')  // discard the invalid input
                ;  // the null statement
            printf("Error! Entered number is not an integer.\n");
            printf("Please enter an integer again.\n");
            val = scanf("%d", numbers + i);
            continue;
        }
        ++i;
    }
    // process the numbers array
    return 0;
}

I write this line again

val = scanf("%d", numbers + i);

Now it works how I need. Great - thanks a lot

这篇关于从数组输入几个数字和整数或不是每一个号检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆