Java 如何编写简单增量的While循环?
我需要为以下内容编写一个WHILE循环:“hello”是输出的一部分 8 9 11 14你好18Java 如何编写简单增量的While循环?,java,while-loop,Java,While Loop,我需要为以下内容编写一个WHILE循环:“hello”是输出的一部分 8 9 11 14你好18 while(counter < 18 ) { System.out.print(" " + counter); counter = counter + 1 ; if(counter > 14 && counter < 18){ System.out.print(" hello
while(counter < 18 )
{
System.out.print(" " + counter);
counter = counter + 1 ;
if(counter > 14 && counter < 18){
System.out.print(" hello ");
}
}
while(计数器<18)
{
系统输出打印(“+计数器);
计数器=计数器+1;
如果(计数器>14和计数器<18){
System.out.print(“hello”);
}
}
以上是我的示例代码。我不知道如何将其增加1、2和3。有人能帮忙吗?您需要一个额外的变量来存储您的增量。在循环的每次运行中,该变量本身必须增加一个。您需要一个额外的变量来存储增量。在循环的每次运行中,此变量本身必须递增一。尝试以下操作:
int counter = 8;
int inc = 1;
while ( counter <= 18 )
{
System.out.print ( " " + counter );
if ( counter >= 14 && counter < 18 )
{
System.out.print ( " hello " );
}
counter = counter + inc;
inc += 1;
}
int计数器=8;
int inc=1;
同时(计数器=14和计数器<18)
{
System.out.print(“你好”);
}
计数器=计数器+有限公司;
inc+=1;
}
试试这个:
int counter = 8;
int inc = 1;
while ( counter <= 18 )
{
System.out.print ( " " + counter );
if ( counter >= 14 && counter < 18 )
{
System.out.print ( " hello " );
}
counter = counter + inc;
inc += 1;
}
int计数器=8;
int inc=1;
同时(计数器=14和计数器<18)
{
System.out.print(“你好”);
}
计数器=计数器+有限公司;
inc+=1;
}
我猜这是家庭作业。如果是,请贴上标签。我的回答含糊不清,因为如果是家庭作业,我不想破坏你的顿悟:)我猜这是家庭作业。如果是,请贴上标签。我的回答含糊不清,因为如果是家庭作业,我不想破坏你的顿悟:)