2012-12-10 45 views
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; 

如何從第一到第n字符(「 - 」)獲得,並與數更換(1)

如果第N爲2 - 程序寫入表11 ---。
如果第二N爲2 - 表1122-

+0

什麼是你想要的輸出程序寫? – arshajii

+0

如果我插入n = 3,表[i] [j] = 111--前3個字符從( - )變爲1 – user1892867

+0

我建議第一次正確地設置問題。 你的每一個編輯都完全改變了問題的含義 準備我的答案兩次,不得不刪除。 –

回答

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()); 
+0

非常感謝你=) – user1892867

+0

不客氣。很高興我能幫上忙 – jonhopkins

相關問題