我正在尝试编写一个程序,提示用户输入一个数字(5 到 50 之间(含)),然后输出到屏幕以给定数字开头的数字倒计时,并包含在同一行上星号的数量。
例如5 ***** 4 **** 3 *** 等等......
我知道我需要一个扫描仪和一个嵌套的 for 循环,但我不确定以哪种方式将它们一起使用,有什么提示吗?
//outer loop
for (int outer = 50; outer <= 5; outer--) {
//inner loop
for (int inner = 1; inner <= outer; inner++) {
我不知道在这种情况下如何开始使用扫描仪!
最佳答案
这可能有帮助:
Scanner scanner = new Scanner(System.in);
int count = scanner.nextInt();
for(int i = count; i > 0; i-- ){
System.out.print(i);
for(int j=i; j>0; j-- ){
System.out.print("*");
}
System.out.println();
}
关于java - 嵌套循环打印一个数字,旁边有相同数量的星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411091/