给数字得到单独的值 [英] Give the number get separate value
本文介绍了给数字得到单独的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于数字10我们得到输出1,2,3,4,5,6,7,8,9,10或5我们得到1,2,3,4,5
解决方案
试试这个
string input = 10;
// input =1-5;
int min = 0 ,max;
string [] items = input.Split(' - '跨度>);
if (items.Length == 2 )
{
max = Convert.ToInt32(items [ 1 ]);
min = Convert.ToInt32(items [ 0 ]);
}
其他
{
min = 1 ;
max = Convert.ToInt32(items [ 0 ]);
}
string output = 跨度>;
for ( int i = min; i < = max; i ++)
输出+ = i + ,;
output = output.TrimEnd(' ,');
Console.WriteLine(输出);
Console.ReadLine();
以下是在C中执行此操作的方法:
/ * 打印由下式确定的自然数序列输入* /
void printNaturals( unsigned int num)
{
/ * 变量声明* /
unsigned int idx;
/ * 确保我们有一个自然数* /
if (num> 0 )
{
for (idx = 1 ; idx< = num; idx ++)
{
/ * 打印无符号整数* /
printf( %u,,idx);
}
/ * 执行内务管理* /
printf( \ n);
}
} / * printNaturals * /
Given the number 10 we get the output 1,2,3,4,5,6,7,8,9,10 or 5 we get 1,2,3,4,5
解决方案
Try this
string input = "10"; //input = "1-5"; int min = 0, max; string[] items = input.Split('-'); if (items.Length == 2) { max = Convert.ToInt32(items[1]); min = Convert.ToInt32(items[0]); } else { min = 1; max = Convert.ToInt32(items[0]); } string output = ""; for (int i = min; i <= max; i++) output += i + ","; output = output.TrimEnd(','); Console.WriteLine(output); Console.ReadLine();
Here is how you would do it in C:
/* Prints a sequence of natural numbers determined by the input */ void printNaturals(unsigned int num) { /* Declaration of variables */ unsigned int idx; /* Ensure we have a natural number */ if (num > 0) { for (idx = 1; idx <= num; idx++) { /* Print unsigned integers */ printf("%u,", idx); } /* Perform housekeeping */ printf("\n"); } } /* printNaturals */
这篇关于给数字得到单独的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文