我是Java新手,我正在使用Tony Mullins编写的Java第一编程课程作为我的学习资源。这是一本非常好的书,但我坚持以下问题:
编写一个打印下表左前n行的过程:
2
2 4
2 4 6
2 4 6 8
编写程序以测试此过程。
我已经弄清楚如何绘制一个带编号的三角形(下图),但我不知道如何使用倍数来获取它2,4,6,8等。
Q也说“前n行”。这是指用户输入的输入吗?
非常感谢任何帮助。
import java.util.Scanner;
public class ExQ4 {
public static void main(String args[]){
// Ask user to enter height
Scanner userInput = new Scanner(System.in);
System.out.println(" Please enter height : ");
int height = userInput.nextInt();
// Pass user input data into method
drawMult (height);
}
// method for performing calculation of user data
public static void drawMult (int height) {
for (int i = 1; i<=height; i++){
// Print space between the numbers
for (int j = 1; j <= i; j++){
System.out.print(height);
}
System.out.println("");
}
}
}