经过 for 循环特定次数,类似于 Python 中的 range() 函数。

huangapple go评论66阅读模式
英文:

Go through for loop specific number of times like range() in python

问题

我终于开始学习 Java 了,在不断地将它与 Python 精英主义进行比较哈哈开玩笑。开玩笑放一边,我期待着用 Java 编码,并且已经在享受其中的乐趣。

在解决问题时,我似乎找不到一个简单的解决方案。基本上,我想创建一个类似于 Python 中的 for 循环,可以重复特定次数,就像这样:

for (int i = 0; i < 10; i++) {
    System.out.println("You win");
}

但当然是针对 Java 的。

经过在互联网上搜索后,我尝试了以下代码:

for (int i : new Range(10)) {
    System.out.println("You win");
}

但是上述代码给我报错:

Range cannot be resolved to a type

希望能得到帮助。

英文:

I finally got around to learning java after continuously bashing it as a python elitist lol.
All jokes aside, I'm looking forward to playing with java and already having some fun with it.

I couldn't seem to find a simple solution for my problem. Basically, I want to make a for loop that will repeat itself a specific amount of times similar to this as in python:

for i in range(10):
    Print(&quot;You win&quot;)

but for java of course.

I tried the following after some digging on the internet:

for (int i : new Range(10)) {
	System.out.println(&quot;You win&quot;);
}

The following code gave me an error saying:

Range cannot be resolved to a type

Any help would be appreciated.

答案1

得分: 1

一个简单的for循环就可以解决问题:

for(int i = 0 ; i < 10; i++){
    System.out.println("你赢了");
}
英文:

A simple for loop would do the trick:

for(int i = 0 ; i &lt; 10; i++){
System.out.println(&quot;you win&quot;);
}

huangapple
  • 本文由 发表于 2020年10月9日 20:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64280023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定