java - 如何在java中从10到1倒序打印计数?

标签 java loops for-loop

我想打印相反的计数顺序。我怎样才能在java中做到这一点? 我尝试了一些代码但没有成功。

import java.util.*;
public class reversecount 
{
    public static void main(String [] args)
    {
        int num;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Any Number");
        num = sc.nextInt();

        for(num=1; num<=10; num--)
        {
            System.out.println(num);
        }
    }
}

最佳答案

您需要从 10(而不是 1)开始,只要该值大于 0 就继续:

for (num = 10; num > 0; num--) {
    System.out.println(num);
}

关于java - 如何在java中从10到1倒序打印计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745726/

相关文章:

java - Spring & Wildfly 异步请求

java - 在面板上显示时钟

java - 使用 Spring 将空字符串转换为空日期对象

c - 为什么中断是停止程序而不是跳回?

c - for循环中的顺序是什么?

java - for 循环内的条件问题 (JAVA)

javascript - 类型错误 : Cannot read property '0' of undefined when comparing the arrays?

java - Spring 参数太长

c# - 在 C# 中循环当前年月到下年月

c++ - 每 x 秒运行一次代码,无论在循环内执行多长时间