Java:如何去掉列表中的一些数字?
我真的不知道该怎么解释,但我可以证明。我想要实现的目标是,第一个循环生成数字1、2、3、4、5。然后第二个循环产生数字1,2,3,4,5,6,7,8,9。我想让第二个循环输出数字6,7,8,9。然后在第三个循环中,它将输出10,11,12,13,14,15。现在我该怎么做呢Java:如何去掉列表中的一些数字?,java,Java,我真的不知道该怎么解释,但我可以证明。我想要实现的目标是,第一个循环生成数字1、2、3、4、5。然后第二个循环产生数字1,2,3,4,5,6,7,8,9。我想让第二个循环输出数字6,7,8,9。然后在第三个循环中,它将输出10,11,12,13,14,15。现在我该怎么做呢 int horse= 5 for (int w =1; w <= horse; w++) { System.out.printl
int horse= 5
for (int w =1; w <= horse; w++)
{
System.out.println(w + " The first loop");
}
int test= horse + 4;
for (int w =1; w <= test; w++)
{
System.out.println(w + " The second loop");
}
int try = test + 6;
for (int w =1; w <= try; w++)
{
System.out.println(w + " The third loop");
}
int horse=5
对于(int w=1;w不要每次都将w
变量重新初始化回1。只需省略它即可
int horse= 5;
int w;
//loop from 1 to 5
for (w =1; w <= horse; w++)
{
System.out.println(w + " The first loop");
}
int test= horse + 4;
//loop from 6 to 9
//here the initial value of w is 6 from the previous loop
for (; w <= test; w++)
{
System.out.println(w + " The second loop");
}
int try0 = test + 6;
//loop from 10 to 15
//here the initial value of w is 10 from the previous loop
for (; w <= try0; w++)
{
System.out.println(w + " The third loop");
}
int-horse=5;
int w;
//从1循环到5
对于(w=1;w这将满足您的需要():
请密切注意您将w
变量设置为什么。让我澄清一下:您想用三个循环而不是一个循环来生成数字1..15?@Kevin,他可能只是在玩游戏,试图自学基础知识。这甚至不会编译…变量w
在第一个for()之后过期
block。太多Python了。谢谢,修复了:)观察到的修订历史。我想我在应用新修订之前看到了新修订…嗯。
// put the increments in an array instead of a scalar.
int[] loops = {5, 4, 6};
String[] names = {"first", "second", "third"};
for(int i = 0, sum = 0; i < loops.length; sum += loops[i++])
for(int j = sum; j < sum + loops[i]; j++)
System.out.println((j + 1) + " The " + names[i] + " loop");
1 The first loop
2 The first loop
3 The first loop
4 The first loop
5 The first loop
6 The second loop
7 The second loop
8 The second loop
9 The second loop
10 The third loop
11 The third loop
12 The third loop
13 The third loop
14 The third loop
15 The third loop