public class id7 { public static void main(String[] args) { int x = 123456; StringBuilder sb = new StringBuilder(x); String count = sb.reverse().toString(); int flag = 0; for (int i = 0; i < count.length(); i++) { char a = count.charAt(i); if(a == '0'){ flag++; }else{ break; } } if(count.length() - flag > 32){ System.out.println(0); }else{ System.out.println(Integer.parseInt(count)); } } } 帮我看看这段代码有什么问题
时间: 2023-04-12 18:00:48 浏览: 97
这段代码的问题在于 StringBuilder 的构造函数只接受字符串类型的参数,而不是整数类型的参数。因此,需要将整数类型的 x 先转换为字符串类型,然后再传入 StringBuilder 的构造函数中。修改后的代码如下:
public class id7 {
public static void main(String[] args) {
int x = 123456;
StringBuilder sb = new StringBuilder(String.valueOf(x));
String count = sb.reverse().toString();
int flag = 0;
for (int i = 0; i < count.length(); i++) {
char a = count.charAt(i);
if(a == '0'){
flag++;
}else{
break;
}
}
if(count.length() - flag > 32){
System.out.println(0);
}else{
System.out.println(Integer.parseInt(count));
}
}
}
阅读全文