1 回答

TA貢獻2051條經驗 獲得超10個贊
您似乎明白,當第一個 while 循環完成時,str.charAt(x)將是一個空格,因此在第二個 while 循環中,您首先檢查str.charAt(x+1),跳過空格。然而,在正文中,您仍在添加str.charAt(x),b這本來是一個空格。
while(str.charAt(x+1) !=(strSpc.charAt(0))){ // x + 1 here
b = b+str.charAt(x); // x here
x++;
}
你應該將其更改為:
while(str.charAt(x+1) !=(strSpc.charAt(0))){ // x + 1 here
b = b+str.charAt(x+1); // x + 1 here as well
x++;
}
第三個 for 循環也是如此。這次,它需要檢查charAt(x+2)并追加charAt(x+2)到c,因為charAt(x+1)那時已經是一個空格了。
x = 0;最后但并非最不重要的一點是,您需要在外循環的每次迭代開始時重置for。
int x = 0; <---- move this....
String str = " ";
for(int i=0; i<loop;i++) {
<---- here
str = JOptionPane.showInputDialog(i+1 + ": ");
事實上,你所做的只是拆分str,可以通過以下方法完成split:
String[] splits = str.split(" ");
int a = Integer.parseInt(splits[0]);
int b = Integer.parseInt(splits[1]);
int c = Integer.parseInt(splits[2]);
a1.add(a);
b1.add(b);
c1.add(c);
添加回答
舉報