编写代码但没有获得我想要的解决方案请帮助 [英] Write A Code But Not Getting Solution That I Want Pls Help

查看:99
本文介绍了编写代码但没有获得我想要的解决方案请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

int main ()
{
   char n[1000];

   printf("Please enter the number of integers you wish to enter\n");
   scanf("%s", n);

   printf("%s", n);

   return 0;
}





我想要的解决方案

请输入您想要输入的整数数量:5

请输入数字:

5

5

5

5

5



Solution that i want
Please enter the number of integers you wish to enter: 5
Please enter the numbers:
5
5
5
5
5

推荐答案

您需要使用两个不同的变量,一个用于保存整数,另一个用于保存输入的值。



例如:

You need to use two different variables, one to hold the number of integers and one array to hold the entered values.

For example:
int numberOfIntegers = 0;
int enteredValues[50];   // This should normally be a dynamic array and you could use
                         // calloc to create enough memory to hold the number of values



获取整数数量的代码:


Code to get the number of integers:

printf("Please enter the number of integers you wish to enter\n");
scanf("%d", &numberOfIntegers);





当你知道整数的数量时,你可以使用一个循环来要求用户输入正确数量的值并将它们存储在数组中。 />


不要忘记检查整数的数量是否小于数组的长度。



When you know the number of integers, you can use a loop to ask the user to input the correct number of values and store them in the array.

Don't forget to check that the number of integers are smaller than the length of the array.


首先是数据类型用于整数值的是int而不是char。请使用%d(控制字符串)接受整数值,%s通常用于char(字符的数据类型)。您可以根据需要使用for循环获取整数。



请使用此参考:



First of all the datatype used for integer values is int and not char.Please use %d(control string) for accepting integer values and %s usually meant for char(datatype for character). You can make use of for loops for getting the integers as per you wish.

Kindly use this for your reference:

#include<stdio.h>

int main ()
{
   int n,i,values;
   printf("Please enter the number of integers you wish to enter\n");
   scanf("%d",&n);
   printf("please enter the numbers");
  for(i=0;i<n;i++)
  {
       scanf("%d",&values);
}
return 0;
}







请注意:



这里我已声明了三个整数变量。变量n用于获取您想要输入的整数,变量i用于执行for循环,直到满足n并使用第三个变量值获取您的意见。

请使用这些链接作为补充参考:

http://fresh2refresh.com/c/c-printf-and-scanf/ [ ^ ]

http://fresh2refresh.com/c/c-data-types/ [ ^ ]


这篇关于编写代码但没有获得我想要的解决方案请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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