如何使用循环在单个变量中存储不同的值。 [英] How to store diffrent values in a single variable using loop.

查看:129
本文介绍了如何使用循环在单个变量中存储不同的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,但是这个代码存在问题,使用for循环在单个变量中存储和记住2个(可能更多)值的问题。

例如 -

  int  a,i; 
for (i = 0 ; i< 2; i ++){
scanf ( %d,& a);
}
for (i = 0 ; i< 2; i ++){
printf( %d,a);
}



让我们使用scanf输入3 5但是它打印5 5.

但我想要3 5怎么做。问题出在哪里。



我尝试过的事情:



In这个程序变量a,应打印输入的值3 5,但它返回5 5,打印时如何记住它,先打印3然后打印5.

解决方案

< blockquote>你必须使用多个变量或 - 如果它们属于同一类型 - 数组:

  #define MY_ARRAY_SIZE 2 

int a [MY_ARRAY_SIZE];
for (i = 0 ; i< MY_ARRAY_SIZE; i ++){
scanf( %d,& a [i]);
}
for (i = 0 ; i< MY_ARRAY_SIZE; i ++) {
printf( %d,a [i]);
}


要么合并2个循环:

  int  a,i; 
for (i = 0 ; i< 2; i ++){
scanf ( %d,& a);
printf( %d,a);
}



您要么了解阵列

C中的数组 [ ^ ]

C阵列:声明,初始化和访问元素(带示例) [ ^ ]

好吧,你可以使用这样的字符数组:



这种类型的结束,但它会起作用。



 int a,i; 
char inval [20],intxt [1000];

intxt [0] = 0; //< == NULL终止字符串
for(i = 0; i< 2; i ++)
{
inval [0] = 0; //< == Null终止输入只是为了安全
printf(输入项目#%0d:,i);
得到(inval);
strcat(intxt,=); //< == some token('=')
strcat(intxt,inval);
}

i = 0;
do
{
if(intxt [i]!= 0)
{
if(intxt [i] =='=')
{
sscanf(& intxt [i + 1],%d,& a);
printf(%d,a);
};
};

} while((intxt [i]!= 0)&&(i< 1000));

printf(\ n);





-Doug


i am trying to code a program but there is a problem in this code,the problem of storing and remembering 2(may be more) values in a single variable using for loop.
e.g-

int a,i;
 for(i=0;i<2;i++){
   scanf("%d",&a);
 }
 for(i=0;i<2;i++){
    printf("%d ",a);
}


let we enter 3 5 using scanf but it prints 5 5.
but i want 3 5 how to do that. where is the problem.

What I have tried:

In this program variable a,should prints the entered values 3 5,but it returns both 5 5,how does it remember it when printing,print 3 first then print 5.

解决方案

You have to use multiple variables or - if they are of same type - an array:

#define MY_ARRAY_SIZE 2

int a[MY_ARRAY_SIZE];
for (i = 0; i < MY_ARRAY_SIZE; i++) {
    scanf("%d", &a[i]);
}
for (i = 0; i < MY_ARRAY_SIZE; i++) {
    printf("%d ", a[i]);
}


Either you merge the 2 loops:

int a,i;
for(i=0;i<2;i++){
    scanf("%d",&a);
    printf("%d ",a);
}


Either you learn about arrays
Arrays in C[^]
C Arrays: Declare, Initialize and Access Elements (With Examples)[^]


Well, you could use a character array like this:

Kind of an end-around, but it would work.

int  a, i;
char inval[20], intxt[1000];

intxt[0] = 0; // <== NULL termination of string
for( i = 0; i < 2 ; i++ )
{
    inval[0] = 0; // <== Null terminate input just for safety
    printf( "Enter Item #%0d:", i ); 
    gets( inval );
    strcat( intxt, "=" );  // <== some token ('=')
    strcat( intxt, inval ); 
}

i = 0;
do
{
    if ( intxt[i] != 0 )
    {
        if ( intxt[i] == '=' )
        {
            sscanf( &intxt[i+1], "%d", &a ); 
            printf( "%d ", a );
        };
    };    
    
} while ( ( intxt[i] != 0 ) && ( i < 1000 ) );

printf( "\n" );



-Doug


这篇关于如何使用循环在单个变量中存储不同的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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