我有兩個類。我希望下面的類中for循環的代碼運行兩次,但是當我發送int i = 0
和條件爲i < 2
時,它似乎只運行一次。正如你所看到的,我必須將它設置爲i < 3
才能讓它運行兩次。Java - for循環問題
import java.util.Scanner;
public class LPrint {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("sender: ");
String a = input.next();
System.out.println("recepiant: ");
String b = input.next();
Letter one = new Letter(a, b);
System.out.println("letter: ");
for (int i = 0; i < 3; i++) {
one.body = one.body.concat(input.nextLine() + "\n");
}
System.out.print(one.getLetter());
}
}
在其他類中,String body = ""
,並且僅在String result = ("to " + recepiant + ":\n").concat(body + "\n").concat("regards,\n").concat(sender);
你應該測試你的循環運行次數的假設......這是很好的調試練習。在循環體中插入一個System.out.println(「Looping ...」)並查看它打印多少次... – Bryan