2

我有

String table[][]= new String [i][j];
Scanner insertN = new Scanner(System.in);
int N= insertN.nextInt ();

table[i][j]="-----";
int number1=1;
int number2=2;

如何从 first 到 n char ("-") 并将其替换为 number(1)

如果第一个 N 是 2 - 程序写入表 11---。
如果第二个 N 为 2 - 程序写入表 1122-

4

1 回答 1

0
String table[][] = new String[i][j];
table[i][j] "-----";
Scanner insertN = new Scanner(System.in);
int number1 = 1;
int number2 = 2;

int N = insertN.nextInt();
int total = N;

//build a temporary string containing as many '1's as indicated by n
String tmp = "";
for (int k = 0; k < N; k++) tmp = tmp + number1;

N = insertN.nextInt();
total = total + N;
for (int k = 0; k < N; k++) tmp = tmp + number2;

//replace the original string with the '1's and the substring of table[i][j]
//starting at position n and continuing to the end
table[i][j] = tmp + table[i][j].substring(total, table[i][j].length());
于 2012-12-10T21:26:55.433 回答