2016-07-02 11 views
-4

私は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 
+7

'if-else'の後ろに' for'ループがあり、両方が返されます。その 'for'ループに到達できません。別の方法でなければならないのでしょうか? –

+0

あなたは余分な '}'を持っています。 – bcsb1001

答えて

0

あなたの問題はファクト(int n)にあります。あなたはif文を使い始めます.nが1の場合は1を返し、そうでない場合はn * fact(n-1)を返します。これは基本的なケースと再帰的なケースを持つ良い再帰関数ですが、すべての条件が既に考慮されているため、決して実行されないコードがあります。 nは1に等しいか、そうではありません!

関連する問題