binaryStr = "0".repeat(padLength) + binaryStr; 改用java8写
时间: 2024-03-11 09:47:52 浏览: 22
好的,如果你使用的是 Java 8,那么可以使用 `String.join` 方法来替代 `repeat` 方法,具体代码如下:
```java
int decimalNum = 10; // 要转换的十进制数
String binaryStr = Integer.toBinaryString(decimalNum); // 转换成二进制字符串
int padLength = 4 - binaryStr.length() % 4; // 计算需要补几个零
if (padLength != 4) {
binaryStr = String.join("", Collections.nCopies(padLength, "0")) + binaryStr; // 在字符串前面补零
}
// 将二进制字符串每 4 位分组,并用空格连接
binaryStr = binaryStr.replaceAll("(?<=\\G.{4})", " ");
```
其中,使用了 `Collections.nCopies` 方法生成一个包含 `padLength` 个 `"0"` 的不可变列表,然后再将它们连接起来,实现了字符串重复的效果。最终输出的结果与之前的示例相同:
```
1010
```
需要注意的是,`Collections.nCopies` 方法可能会占用较多的内存,因为它会在内存中存储多个相同的对象。如果需要重复的次数比较大,或者要重复的字符串比较长,建议使用其他方法来实现。
相关问题
import java.util.Scanner; public class ZhunBei_1{ public static void main(String[]args) { int ri,repeat; int i,j,n,a[][]=new int[10][10]; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1;ri<=repeat;ri++) { n=in.nextInt(); for(i=1;i<=n;i++) { a[0][i]=i; a[i][0]=i; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) { a[i][j]=i+j; } for(i=0;i<n;i++) { for(j=0;j<=n;j++) if(i==0&&j==0) System.out.print("+"); else if(i==0||j<=i) System.out.print(a[i][j]+""); System.out.println(); } } } } 优化代码
以下是对代码的优化:
1. 变量声明和初始化可以在同一行完成,比如:int ri = 1, repeat = in.nextInt();
2. 变量名可以更具体一些,比如:rowIndex, colIndex, size,而不是 i, j, n。
3. 数组初始化时可以使用 for 循环,而不是分别赋值。
4. 输出时可以使用 printf() 方法,格式化输出。
优化后的代码如下:
import java.util.Scanner;
public class ZhunBei_1 {
public static void main(String[] args) {
int rowIndex = 1, colIndex = 1, size;
int[][] array;
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
for (int i = 0; i < repeat; i++) {
size = in.nextInt();
array = new int[size + 1][size + 1];
for (rowIndex = 1; rowIndex <= size; rowIndex++) {
array[0][rowIndex] = rowIndex;
array[rowIndex][0] = rowIndex;
}
for (rowIndex = 1; rowIndex <= size; rowIndex++) {
for (colIndex = 1; colIndex <= size; colIndex++) {
array[rowIndex][colIndex] = rowIndex + colIndex;
}
}
for (rowIndex = 0; rowIndex <= size; rowIndex++) {
for (colIndex = 0; colIndex <= size; colIndex++) {
if (rowIndex == 0 && colIndex == 0) {
System.out.print("+");
} else if (rowIndex == 0 || colIndex <= rowIndex) {
System.out.printf("%3d", array[rowIndex][colIndex]);
}
}
System.out.println();
}
}
}
}
package KaoShi; import java.util.Scanner; public class ZhunBei_1 { public static void main(String []args) { int ri, repeat; int i,j,n,a[][]=new int[10][10]; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri<=repeat; ri++) { n=in.nextInt(); for(i=1;i<n;i++) { a[0][i]=i; a[i][0]=i; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { a[i][j]=i+j; } for( i=0; i<n; i++) { for( j=0;j<=n; j++) if(i==0&&j==0) System.out.print( "+ "); else if(i==0||j<=i) System.out.print(a[i][j]+" "); System.out.println(); } } } } 找错误
这段代码存在以下错误:
1. 变量 i 在第一个 for 循环中被重复定义,需要将第二个 for 循环中的 i 改为 k 或其他未使用的变量名。
2. 第二个 for 循环中的花括号位置不正确,应该放在 a[0][i]=i; a[i][0]=i; 的后面,而不是整个循环的后面。
3. 第三个 for 循环中的 j<=n 应该改为 j<n,否则会越界。
4. 输出格式不太美观,可以在每个数字之间添加空格或制表符,以便对齐。
修改后的代码如下:
```
package KaoShi;
import java.util.Scanner;
public class ZhunBei_1 {
public static void main(String []args) {
int ri, repeat;
int i,j,k,n;
int a[][] = new int[10][10];
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
n = in.nextInt();
for (i = 1; i < n; i++) {
a[0][i] = i;
a[i][0] = i;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
a[i][j] = i + j;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == 0 && j == 0) {
System.out.print("+\t");
} else if (i == 0 || j <= i) {
System.out.print(a[i][j] + "\t");
}
}
System.out.println();
}
}
}
}
```
值得注意的是,这段代码存在一个问题:当输入的 n 大于 10 时,数组下标会越界。因此,需要在程序中添加一些判断和限制条件,以确保程序的正确性和鲁棒性。