Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我一直收到这个错误消息无法访问的代码,为什么?这是我的密码_Java - Fatal编程技术网

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

Java 我一直收到这个错误消息无法访问的代码,为什么?这是我的密码,java,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

我一直收到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
import java.util.Scanner;
公共类阶乘{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int n;
int产品;
System.out.println(“欢迎使用阶乘计算器!”);
目标反应;
做{
System.out.print(“输入一个大于0但小于10的整数:”;
n=input.nextInt();
产品=事实(n);
System.out.println(““+n”的阶乘);
System.out.println(“is”+产品);
系统输出打印(“是否继续?”);
response=input.next();
}而(response.equals(“y”)| response.equals(“y”);
}
公共静态整数事实(整数n){
如果(n==1){
返回1;
}否则
返回n*事实(n-1);
对于(int i=0;i<10;i++){
int结果=结果*i;
int random=(int)(Math.random()*10);
如果(随机>10){
System.out.println(“无效值-继续循环!”);
继续;
//继续执行循环,直到应用程序结束
}
系统输出打印项次(随机);
}
}
}
}//这个大括号就是我一直得到错误代码的地方

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

if-else
之后有一个
for
循环,两者都返回。无法访问循环的
。应该用另一种方法吗?您还有一个额外的
}