簡體   English   中英

我正在通過循環方法查找數字的階乘,但是當我嘗試運行代碼時,出現錯誤,因為“我”無法在變量中解析

[英]i'm finding factorial of a number by loop method but when I am trying to run the code, there's an error as "i' cannot be resolved in a variable

package loops;

import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int factorial = 1;

        for(int i = n ; i>=1 ; i--); {

             factorial = factorial * i;

        }

        System.out.println(factorial);
    }

 }

您的程序包含一個; 在 for 循環旁邊,它導致循環變量i不在程序中定義的階乘方程的 scope 中。 刪除; 它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM