gpt4 book ai didi

Java:如何删除列表中的某些数字?

转载 作者:行者123 更新时间:2023-12-01 12:54:42 24 4
gpt4 key购买 nike

我真的不知道如何解释这一点,但我可以展示。我想要实现的目标是第一个循环产生数字 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.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");
}

最佳答案

不要每次都将 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");
}

请注意,try 是保留的系统关键字,因此请将其重命名为 try0

关于Java:如何删除列表中的某些数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23986689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com