【问题标题】:While Loops and Reverse FibonacciWhile循环和反向斐波那契
【发布时间】:2013-10-09 19:52:26
【问题描述】:

我遇到了一个问题。我正在尝试创建一个类,它采用用户输入的最大数字并在它之前添加整数直到它变为 0,但是,当我运行它时,数字会越来越大,直到它崩溃。是什么让它陷入无限循环?

public class Summation {

    public static void main(String[] args) {
        EasyReader console = new EasyReader();
        System.out.print("Debug? (Y/N): ");
        char debug = console.readChar();    
        if ((debug!='Y')&&(debug!='N')){
            System.out.println("Please enter Y or N");
            main(null); 
        }
        else{

            System.out.print("Enter max range:");
            int max = console.readInt();
            int s = sum(max,debug);     
            System.out.print(s);
        }
    }
    public static int sum(int m, char d){
        int sm = 1;
        boolean isRunning = true;
        while ((isRunning == true)&&(d=='Y')){
            if ((--m)==0) {
                isRunning = false;
                }
            else{
                sm = m+(--m);
                System.out.println("sm is"+sm);
                }
        while ((isRunning == true)&&(d=='N')){
            if ((--m)==0) {
                isRunning = false;
                }
            else{
                sm = m+(--m);
                }           
            }
        }return sm;
    }
}

【问题讨论】:

  • 所以您将两个正整数相加,然后惊讶于结果大于?你真的考虑清楚了吗?这与编程无关,您的代码完美无缺:它完全按照指定的方式运行。这是PEBKAC。
  • @ppeterka66 其中一个整数应该倒计时直到它到达 0,并且该过程应该在那个点终止。
  • 次要注意,不要在你的条件下使用isRunning == trueisRunning 就足够了,它可以防止你错误地将布尔值分配给像isRunning = true 这样的变量。
  • @Showman - 那么问题是这与斐波那契没有任何联系...这只是 1 和最大值之间的所有数字的总和,我没记错,这可以通过 @987654325 解决@, n 是最大数 - 不需要单个循环。并且错误很可能是 if 语句的评估减少 m,而 else 语句再次减少它......
  • 输入 'Y' 和 501 对我来说终止正常。你给它什么输入?另外,嵌套while 的目的是什么?最外层的while 只会在 d=='Y' 时输入,所以嵌套循环永远不会做任何事情。

标签: java loops while-loop


【解决方案1】:

有一个场景,你的退出条件

if ((--m)==0)

将永远不会再到达,因为m 已经小于 0,并且永远不会返回。

只要m 是偶数,就会出现这种情况。

while ((isRunning == true)&&(d=='Y'))
{
    // this condition decriments `m` every time it runs, regardless of whether it evaluates to true
    if ((--m)==0) 
    {
        // if `m` was set to 0 on your last iteration, it will be set to -1
        isRunning = false;
    }
    else
    {
        // if m is 1 before this line it will be 0 after it.
        sm = m+(--m);
        System.out.println("sm is"+sm);
    }
    while ((isRunning == true)&&(d=='N'))
    {
        // this code will never get executed       
    }
}
return sm;

【讨论】:

    【解决方案2】:

    您的问题的答案很简单 修改条件即可

    if (m==0) {
        isRunning = false;
    }
    

    当您检查--m == 0 时,m 很有可能会跳过 0 并进入负区域,甚至无需将此条件设置为 true。

    【讨论】:

      【解决方案3】:

      你所做的一切都是错的:)。

      首先 - 格式化。你甚至可能不知道,但第二个 while 是在第一个 while 循环内。如果你使用netbeans,它的ALT+SHIFT+F。

      使用 --m 对您的示例不利,因为它首先减小“m”值,然后进行比较。所以即使你在

      (--m)==0
      

      你减少了一个值。而且因为您在

      再次使用它
      sm = m+(--m)
      

      您甚至可以跳过“0”值并输入负数。

      但是,如果您只想“在 while 循环中以相反的顺序将数字从给定数字添加到 0”,则它不是斐波那契,您可以使用此代码(它可以做得更好,但这是使用您的代码):

      public class Summation {
      
          public static void main(String[] args) {
              System.out.println(sum(10, 'Y'));
          }
      
          public static int sum(int m, char d) {
              int sm = 0;
              boolean isRunning = true;
              while ((isRunning == true) && (d == 'Y')) {
                  sm += m;
                  if (m == 0) {
                      isRunning = false;
                  } else {
                      m--;
                      System.out.println("sm is" + sm);
                  }
                  while ((isRunning == true) && (d == 'N')) {
                      if ((--m) == 0) {
                          isRunning = false;
                      } else {
                          sm = m + (--m);
                      }
                  }
              }
              return sm;
          }
      }
      

      请注意,无法达到第二个 while 循环 - 它仅在“d == Y”时通过,然后仅在“d == N”时开始

      【讨论】:

        猜你喜欢
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 2013-10-19
        • 2014-01-12
        • 2015-04-30
        • 2016-01-02
        • 2020-01-26
        相关资源
        最近更新 更多