![](/img/trans.png)
[英]I need to write a while loop that will iterate 10 times with a value of count from 1 to 10 in java
[英]Write a while loop that will iterate 10 times with a value of count from 1 to 10
我尝试了很多东西,但没有一个有效。 我知道这个练习很简单,但我无法让它打印 1 到 10 十次。 任何帮助将不胜感激。
public static void main(String[] args) {
int num1 = 1;
while(num1 <= 10){
System.out.println(num1);
num1++;
}
}
1 2 3 4 5 6 7 8 9 10 这只是我跑步时得到的
public static void main(String[] args) {
int num1 = 1;
for (int i = 0; i < 10; i++) {
while (num1 <= 10) {
System.out.println(num1);
num1++;
}
}
}
在打印编号 1 到 10 循环之外再添加一个循环以打印十次
您可以使用 Intstream 范围在您的 while 循环中迭代 1 到 10 次。 像这样:
import java.util.stream.IntStream;
public class Main
{
public static void main(String[] args)
{
int num1 = 1;
while(num1 <= 10){
IntStream stream = IntStream.range(1, 11);
stream.forEach(System.out::println);
num1++;
}
}
}
假设任务是生产:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
只使用一个while循环,你可以用2个整数来做,基本上一个跟踪10的计数,一个跟踪单位。
public static void main(String args[]) {
int i=0;
int j=0;
while (j<10)
{
i++;
System.out.print(i + " ");
if (i==10){
i=0;
j++;
System.out.println();
}
}
}
public class Main
{
public static void main(String[] args)
{
int iteration = 1;
while(iteration<=10)
{
int count= 1;
while(count<=10)
{
System.out.print(count + " ");
count++;
}
System.out.print("\n");
iteration++;
}
}
}
有两个while循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.