好吧,你可以使用这样的字符数组: 这种类型的结束,但它会起作用。 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 2int 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 arraysArrays 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 stringfor( 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 这篇关于如何使用循环在单个变量中存储不同的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-09 02:32