所以我需要使用for循環顯示三角形..出於某種原因代碼正在編譯,但是當我運行程序時什麼也沒有發生。循環是無用的,但我必須使用for循環來顯示它作爲我的練習的一部分。在for循環中使用方法
public class TrianglesUsingLoops {
public static void main(String[] args) {
for (int n = 0;n>=2;n++){
upperTriangle();
lowerTriangle();
}
}
public static void upperTriangle(){
System.out.println(" * ");
System.out.println(" *** ");
System.out.println("*****");
}
public static void lowerTriangle(){
System.out.println("*****");
System.out.println(" *** ");
System.out.println(" * ");
}
}
I thi你的意思是你的循環中'n <= 2'而不是'n> = 2'。 – Tunaki
由於上述原因,您的循環會執行零次迭代,因爲終止條件在開始時就已滿足。 –