可能性の重複:
Display numbers from 1 to 100 without loops or conditions印刷1
インタビューの質問:Javaで任意のループのない10
印刷1。
可能性の重複:
Display numbers from 1 to 100 without loops or conditions印刷1
インタビューの質問:Javaで任意のループのない10
印刷1。
簡単な方法:System.out.println
値:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
System.out.println(10);
複雑な方法:使用再帰
public void recursiveMe(int n) {
if(n <= 10) {// 10 is the max limit
System.out.println(n);//print n
recursiveMe(n+1);//call recursiveMe with n=n+1
}
}
recursiveMe(1); // call the function with 1.
これは26秒間にどうやってやったのですか? – Mysticial
Eclipseのため! :p私はEclipseが大好きです。 –
@ハリージョイ:あなたの日食は、光の速度で動作します...:D –
あなたのプログラム鈍角、無ループ、条件文やmainメソッドのような場合。
static int i = 0;
static {
try {
recurse();
} catch (Throwable t) {
System.exit(0);
}
}
private static void recurse() {
System.out.print(++i + 0/(i - 11) + " ");
recurse();
}
これは、ループを使用していますが、あなたはループを使用しないように、これを再構築することができ、興味深い答え
Random random = new Random(-6732303926L);
for(int i = 0; i < 10; i++)
System.out.print((1+random.nextInt(10))+" ");
}
かもしれません。
鮮やかな答え+1 –
System.out.println( "1〜10"); ...:D –
* "インタビュー質問:" *あなたの*答えは何ですか?インタビュアーが返す前に、あなたのモバイルにこの質問を入力して、答えを期待していますか? –