我一直收到这个错误消息无法访问的代码,为什么会这样?这是我的代码

时间:2016-07-02 22:10:18

标签: java

我一直在Java无法访问的代码中收到错误消息。这是我的代码:

import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n;
        int product;
        System.out.println("Welcome to the Factorial Calculator!");

        Object response;
        do {
            System.out.print("Enter an integer that's greater than 0 but less than 10:");
            n = input.nextInt();
            product = fact(n);
            System.out.println("The factorial of " + n);
            System.out.println("is " + product);
            System.out.print("Continue?(y/n): ");
            response = input.next();
        } while (response.equals("y") || response.equals("Y"));
    }

    public static int fact(int n) {
        if (n == 1) {
            return 1;
        } else
            return n * fact(n - 1);

        for (int i = 0; i < 10; i++) {
            int result = result * i;
            int random = (int) (Math.random() * 10);
            if (random > 10) {
                System.out.println("invalid value - continue loop!");
                continue;
                // continue executing loop until the application ends
            }

            System.out.println(random);
        }
    }
}
} // This curly brace is where I keep getting the error code

1 个答案:

答案 0 :(得分:0)

您的问题出在函数fact(int n)中。你从一个if语句开始 - 你说如果n等于1,则返回1,否则返回n * fact(n-1)。这是一个很好的递归函数,有一个基本案例和一个递归的情况,但你下面的代码永远不会执行,因为所有的条件都已经考虑过了。 n等于1,或者不等于!