我如何制作我的代码
class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.println(fact);
}
}
}
看起来像这样
X X!
1 1
2 2
3 6
4 24
5 120
答案 0 :(得分:5)
System.out.println(c+" "+fact);
答案 1 :(得分:2)
您可以使用以下代码。
您可以让System.out.println()在新行上打印它 和System.out.print()在同一行上打印东西。
class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
System.out.println("x x!");
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.println(c + " " +fact);
}
}
}
答案 2 :(得分:2)
又一个解决方案:
private static final String TEMPLATE = "%s %s";
public static void main(String args[]) {
int n, c, fact = 1;
System.out.println(String.format(TEMPLATE, "X", "X!"));
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.println(String.format(TEMPLATE, c, fact));
}
}
答案 3 :(得分:1)
试试这个:
public static void main(String args[]) {
int n, c, fact = 1;
System.out.println("X X! ");
for (c = 1; c <= 5; c++) {
fact = fact * c;
System.out.println(c + " " + fact);
}
答案 4 :(得分:1)
System.out.println(c+" "+fact);
答案 5 :(得分:0)
试
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.println(c + " " +fact);
}
答案 6 :(得分:0)
class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.print(c+"! = "+fact+(c==5?".":", "));
}
}
}
答案 7 :(得分:-1)
不确定您是在询问是否在一行中格式化输出,或者您想要进行一些逻辑更改:
为了格式化一行中的输出,这里是更新的代码:
public class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
for ( c = 1 ; c <= 5 ; c++ ) {
fact = fact*c;
System.out.print(fact);
System.out.print(" ");
}
}
}