这个算法题做不出来
要求是只能用while if循环
本人刚学不会做 希望有解答
public class ShowCube {
public static void showCubes(int lower,int upper){
//循环结束标志
boolean isEnd = false;
boolean hasCube = false;
//结果字符串
StringBuilder sb = new StringBuilder();
//循环初始值
int currentNumber = lower;
while(!isEnd){
//计算立方
int cube = currentNumber*currentNumber*currentNumber;
if(cube>=lower&&cube<=upper){
hasCube = true;
sb.append(" ").append(currentNumber).append("^3=").append(cube).append(" <");
} else if(cube>upper){
isEnd = true;
}
currentNumber+=1;
if(currentNumber>upper){
break;
}
}
if(hasCube){
System.out.println("The cubes in["+lower+","+upper+"] are:");
System.out.println(lower+" <="+sb.toString()+"= "+upper);
} else{
System.out.println("There are no cubes in["+lower+","+upper+"]");
}
}
public static void main(String[] args) {
showCubes(1,30);
showCubes(10,20);
}
}