谁能告诉我如何将 x 和 y 的值更改为“*”和“.”?将变量设置为字符串无济于事,因为我需要 N 是一个整数,因为它是用户输入,并且会在循环中发生冲突。
import java.util.Scanner;
public class nvalue
{
public static void main(String[] args)
{
int x, y, N;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Value of N : ");
N = keyboard.nextInt();
for(x=2; x<=N; x++)
{
for(y=1; y<=5; y++)
{
System.out.println(y +" * "+ x + " = " + (x*y));
}
System.out.println("----------------------");
}
}
}
基本上我需要它来输出这个:
* * * * * *
. * * * * *
. . * * * *
. . . * * *
. . . . * *
. . . . . *
最佳答案
你可以这样做:
for (int i = 0; i < N; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print(".");
}
for (int j = i; j < N; ++j) {
System.out.print("*");
}
System.out.println(); // Next line
}
请注意使用 print
而不是 println
进行无换行打印。
关于java - 对于循环整数和字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538104/