4 回答
![?](http://img1.sycdn.imooc.com/546353070001b3eb01000097-100-100.jpg)
TA贡献564条经验 获得超863个赞
public static boolean isPrime(int x) { boolean flag = true; if (x < 2) { return false; } else { for (int i = 2; i <= Math.sqrt(x); i++) { if (x % i == 0) { flag = false; break; } } } return flag; } public static String rightAlign(char[][] x) { String result = ""; if (x != null && x.length > 0) { // 计算每行内容最大的长度 int maxLine = 0; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { if (x[i].length > maxLine) { maxLine = x[i].length; } } } for (int i = 0; i < x.length; i++) { // 拼空格 if (x[i].length < maxLine) { int a = maxLine - x[i].length; for (int k = 0; k < a; k++) { result += " "; } } // 拼字符 for (int j = 0; j < x[i].length; j++) { result += x[i][j]; } // 换行 result += "\n"; } } return result; }
![?](http://img1.sycdn.imooc.com/5472ee6d0001411b01000100-100-100.jpg)
TA贡献34条经验 获得超22个赞
public static String rightAlign(char[][] x){
String result = "";
int maxLength = 0;
for(char[] c:x){//遍历二维数组,找出数组中的最大长度
if(c.length > maxLength)
maxLength = c.length;
}
for (char[] c: x){//遍历二维数组
for(int i = 0; i < maxLength - c.length; i++){//如果数组的长度比最大长度小,在此前面补空格,实现右对齐
result += " ";
}
for(char car: c){//遍历一维数组
result += car;
}
result += "\n";
}
return result;
}
![?](http://img1.sycdn.imooc.com/55389d2000017d3601860186-100-100.jpg)
TA贡献106条经验 获得超23个赞
1.
public static boolean isPrime(int x){
if(x==1){
return true;
}else if(x<1){return false;}
else{for(int i = 2;i<i/1.3;i++){
if(x%i==0){
return false;}else return true;
}}
}
![?](http://img1.sycdn.imooc.com/5b0d98d50001b0cb06400640-100-100.jpg)
TA贡献3条经验 获得超0个赞
public static String rightAlign(char[][] x){
String result = "";
int maxLength = 0;
for(char[] c:x){
if(c.length > maxLength)
maxLength = c.length;
}
for (char[] c: x){
for(int i = 0; i < maxLength - c.length; i++){
result += " ";
}
for(char car: c){
result += car;
}
result += "\n";
}
return result;
}
添加回答
举报