如何使用 C# 在不循环的情况下打印 1 到 100
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1810028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print 1 to 100 without any looping using C#
提问by rahul
I am trying to print numbers from 1 to 100 without using loops, using C#. Any clues?
我正在尝试使用 C# 在不使用循环的情况下打印从 1 到 100 的数字。有什么线索吗?
采纳答案by Caleb
Recursion maybe?
可能是递归?
public static void PrintNext(i) {
if (i <= 100) {
Console.Write(i + " ");
PrintNext(i + 1);
}
}
public static void Main() {
PrintNext(1);
}
回答by FrustratedWithFormsDesigner
Console.WriteLine('1');
Console.WriteLine('2');
...
Console.WriteLine('100');
...Or would you have accepted a recursive solution?
...或者你会接受递归解决方案吗?
EDIT: or you could do this and use a variable:
编辑:或者你可以这样做并使用一个变量:
int x = 1;
Console.WriteLine(x);
x+=1;
Console.WriteLine('2');
x+=1;
...
x+=1
Console.WriteLine('100');
回答by ojrac
I can think of two ways. One of them involves about 100 lines of code!
我可以想到两种方法。其中之一涉及大约 100 行代码!
There's another way to reuse a bit of code several times without using a while/for loop...
还有另一种方法可以在不使用 while/for 循环的情况下多次重用一些代码......
Hint: Make a function that prints the numbers from 1 to N. It should be easy to make it work for N = 1. Then think about how to make it work for N = 2.
提示:制作一个函数,打印从 1 到 N 的数字。让它在 N = 1 时工作应该很容易。然后考虑如何让它在 N = 2 时工作。
回答by Donnie
Console.Out.WriteLine('1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100');
回答by Hyman
I can think two ways:
我可以想到两种方式:
- using 100
Console.WriteLine
- using
goto
in aswitch
statement
- 使用 100
Console.WriteLine
- 用
goto
在switch
声明
回答by wallyk
Method A:
方法一:
Console.WriteLine('1');
Console.WriteLine('print 2');
Console.WriteLine('print 3');
...
Console.WriteLine('print 100');
Method B:
方法B:
func x (int j)
{
Console.WriteLine(j);
if (j < 100)
x (j+1);
}
x(1);
回答by jason
Enumerable.Range(1, 100)
.Select(i => i.ToString())
.ToList()
.ForEach(s => Console.WriteLine(s));
Not sure if this counts as the loop is kind of hidden, but if it's legit it's an idiomatic solution to the problem. Otherwise you can do this.
不确定这是否算作循环有点隐藏,但如果它是合法的,它就是解决问题的惯用方法。否则你可以这样做。
int count = 1;
top:
if (count > 100) { goto bottom; }
Console.WriteLine(count++);
goto top;
bottom:
Of course, this is effectively what a loop will be translated to anyway but it's certainly frowned upon these days to write code like this.
当然,这实际上是循环将被翻译成的内容,但是现在编写这样的代码肯定是不受欢迎的。
回答by brendan
public void Main()
{
printNumber(1);
}
private void printNumber(int x)
{
Console.WriteLine(x.ToString());
if(x<101)
{
x+=1;
printNumber(x);
}
}
回答by Faisal Abid
This is more or less pseudo code I havent done c# in years, PS running on 1 hour of sleep so i might be wrong.
这或多或少是我多年来没有做过 c# 的伪代码,PS 在 1 小时的睡眠中运行,所以我可能是错的。
int i = 0;
public void printNum(j){
if(j > 100){
break;
} else {
print(j);
printNum(j + 1);
}
}
public void main(){
print(i);
printNum(i + 1);
}
回答by Raj
PrintNum(1);
private void PrintNum(int i)
{
Console.WriteLine("{0}", i);
if(i < 100)
{
PrintNum(i+1);
}
}