Java 更具表现力的代码
是否有一种更智能的方法来编写此代码,以避免重复条件(答案将其作为一个for循环,并具有适当的边界(从4开始,因为Java 更具表现力的代码,java,logic,recompile,Java,Logic,Recompile,是否有一种更智能的方法来编写此代码,以避免重复条件(答案将其作为一个for循环,并具有适当的边界(从4开始,因为4*4=16>15和3*3=915和3*3=9
4*4=16>15
和3*3=9<15
;从14结束,因为14*14=196<200
和15*15=225>200
):
for (int i = 4; i <= 14; ++i) {
System.out.println(i*i);
}
for(int i=4;i将其写为一个for循环,具有适当的边界(从4开始,因为4*4=16>15
和3*3=9<15
;从14结束,因为14*14=196<200
和15*15=225>200
:
for (int i = 4; i <= 14; ++i) {
System.out.println(i*i);
}
for(int i=4;i如果希望保持while循环,可以执行以下操作:
public class HelloWorld {
public static void main(String[] args){
perfectSquareBetweenFifteenAndTwoHundred();
}
void perfectSquareBetweenFifteenAndTwoHundred(){
int i=4;
int answer=0;
while(answer<200){
System.out.println(answer);
answer = i*i;
i++;
}
}
}
公共类HelloWorld{
公共静态void main(字符串[]args){
PerfectSquareBetweenfifteenandWohund();
}
void PerfectSquareBeween Fifteenand和Wohund(){
int i=4;
int-answer=0;
while(回答如果希望保持while循环,可以执行以下操作:
public class HelloWorld {
public static void main(String[] args){
perfectSquareBetweenFifteenAndTwoHundred();
}
void perfectSquareBetweenFifteenAndTwoHundred(){
int i=4;
int answer=0;
while(answer<200){
System.out.println(answer);
answer = i*i;
i++;
}
}
}
公共类HelloWorld{
公共静态void main(字符串[]args){
PerfectSquareBetweenfifteenandWohund();
}
void PerfectSquareBeween Fifteenand和Wohund(){
int i=4;
int-answer=0;
我想你在这里不是指“编译”,也许是指“表达”或“写”,我想你在这里不是指“编译”,也许是指“表达”或“写”。
public class MyClass {
public static void main(String[] args) {
for (int i = 1; true; i++) {
int square = i * i;
if (square > 200) {
return;
}
if (square > 15) {
System.out.println(square);
}
}
}
}