【问题标题】:Result no initialized?结果没有初始化?
【发布时间】:2013-11-06 05:38:25
【问题描述】:
import java.util.*;

//Creates a program which allows the user to find the factorial of a number

public class forLoop {
    public static void main (String [] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the number you want the factorial from: ");
        int number = input.nextInt(); // user input
        input.close();

        int result_2 = getFactorial(number); //initializes getFactorial method
        System.out.println("The factorial of " + number + " is " + result); //prints results

        }

    public static int getFactorial (int num1) {
        int result;

        for (int times = num1; times <= 1; times--) { //repeats loop until times <=1

            result = num1 * (num1 - 1); //does the factorial equation

        }

        return result;  // returns results (here is the problem)
    }
}

【问题讨论】:

    标签: java loops for-loop methods return


    【解决方案1】:

    编译器不能假定循环将至少执行一次 - 这是分配result 的必要条件。

    修改result的声明如下解决问题:

    int result = 1;
    

    这将有助于您的代码编译,但它不会修复计算阶乘时的逻辑错误:目前,您的循环会因为错误的循环条件而无限期地运行。

    您应该将 1num1 之间的数字相乘(含)。将循环条件更改为times &gt;= 1 而不是times &lt;= 1,并将循环主体更改为result *= times 以修复此错误。

    【讨论】:

    • IMO,int result = 1; 可能会更好,因为该程序是关于阶乘的。并且主要方法中还有另一个错误。打印最终结果的 SOP。
    • @R.J 这里有一个逻辑错误,我会再写一些。
    【解决方案2】:

    你需要初始化这个变量:

    int result;
    

    像这样:

    int result = 0; //Which ever intial value you want
    

    因为编译器无法确定 for 循环是否会一直执行。

    【讨论】:

      【解决方案3】:

      for循环的条件不正确

      应该是

      for (int times = num1; times >= 1; times--)
      {
      
                  result *= times; //this wil calculate right factorial
      
      }
      

      在 for 循环之前也将 result 初始化为 1

      【讨论】:

        【解决方案4】:

        由于您将函数返回值分配给result_2,因此您应该打印它而不是结果

        试试

        System.out.println("The factorial of " + number + " is " + result_2); 
        

        并且你需要在使用它们之前初始化局部变量

        【讨论】:

          【解决方案5】:
          int result;
          
              for (int times = num1; times <= 1; times--) { //repeats loop until times <=1
          
                  result = num1 * (num1 - 1); //does the factorial equation
          
              }
          

          此块导致错误。如果由于这个原因循环没有运行一次

          条件 java 不会在这里打印任何东西

          System.out.println("The factorial of " + number + " is " + result);
          

          所以这里需要初始化,它作为打印的默认值。 所以解决办法是更换

          int result;
          

          int result=1; // note that I am not initializing with 0 as that will cause every factorial to become zero.
          

          您的代码中有另一个错误,而不是

          times <= 1
          

          应该是

          times >= 1
          

          你的代码可能不会因为这个错误而运行一次。

          【讨论】:

            【解决方案6】:

            只需将int result 初始化为:

            int result = 0;
            

            因为您的循环没有执行(times 已经大于 1),它试图返回一个未初始化的变量。

            【讨论】:

              猜你喜欢
              • 2020-03-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-08-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多