【问题标题】:java not saving ints correctlyjava没有正确保存整数
【发布时间】:2017-02-27 23:00:49
【问题描述】:

此程序适用于 Java。它应该做Collat​​z猜想。在我的研究中,代码应该可以工作,但是当我输入 7 时,它会打印出一堆 22,我认为这是 java 保存整数的问题。

import java.util.*;
//If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3  .    and add 1
public class infNum {
   private int num;
   private int n;
   private String comma = ", ";

   public void start() {
      System.out.println("enter a number");
      Scanner keyboard = new Scanner (System.in);
      int n = keyboard.nextInt();
      num = n;
   }
   public void testEvenOdd() {
       if((num % 2) == 0) {
          ifEven(num);
      } else {
         ifOdd(num);
      }
   }

     public void ifEven(int num) {
      if(num == 1) {
         return;
      } else {
         num = num / 2;
         System.out.print(num + comma);
         testEvenOdd();
      }
   }

   public void ifOdd(int num) {
      if(num == 1) {
         return;
      } else {
         num = (num * 3) +1;
         System.out.print(num + comma);
         testEvenOdd();
      }
   }
}

【问题讨论】:

  • 嗯,7 是一个奇数。 7 * 3 是 21,21 + 1 22。看起来你的程序完全按照你说的做了。
  • 这个怎么称呼?开始方法在哪里,如果是开始,其他方法都不会被调用?
  • 这更多的是您的testEvenOdd 方法检查num 实例变量的问题,而您的其他方法永远不会更新它。目前他们只更新自己的参数,当方法结束时超出范围。
  • 提示:num 在您的ifEvenifOdd 方法中引用作为参数传递的局部变量 num

标签: java


【解决方案1】:

代码应该可以工作,但是当我输入 7 时,它会打印出一堆 22 我认为这是 java 保存整数的问题。

问题似乎是您将实例变量与局部变量 i.e 混合在一起,您想更新实例变量,而是更新了局部变量。这个很容易解决,用this关键字区分就行了。

例子:

import java.util.*;
    //If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3  .    and add 1
    public class infNum {
       private int num;
       private int n;
       private String comma = ", ";

   public void start() {
      System.out.println("enter a number");
      Scanner keyboard = new Scanner (System.in);
      int n = keyboard.nextInt();
      this.num = n;
   }
   public void testEvenOdd() {
       if((this.num % 2) == 0) {
          ifEven(this.num);
      } else {
         ifOdd(this.num);
      }
   }

     public void ifEven() {
      if(this.num == 1) {
         return;
      } else {
         this.num = this.num / 2;
         System.out.print(this.num + comma);
         testEvenOdd();
      }
   }

   public void ifOdd() {
      if(this.num == 1) {
         return;
      } else {
         this.num = (this.num * 3) +1;
         System.out.print(this.num + comma);
         testEvenOdd();
      }
   }
}

更新

我已经更新了您的方法参数,因为由于使用了实例变量,因此无需将num 作为参数传递给ifEvenifOdd。这将消除您当前遇到的错误。另外,请注意,由于我们不再有与实例变量同名的方法参数,因此我们不再需要 this 关键字。但是,我将它们留在那里以进行良好的练习。

【讨论】:

  • 扮演魔鬼的倡导者,为什么将int num传递给那些方法?
  • @OusmaneDiaw 由于使用了实例变量,因此无需将num 作为参数传递给ifEvenifOdd(我认为这也是Elliott 所说的)
  • @BackSlash 正确我明白他的意思了。我应该更新我的答案以适应这些建议。
【解决方案2】:

如果你的输入是 7,那么 22 就是正确的输出!

testEvenOdd() 转到 else 语句,即 ifOdd(7)。在这里,由于 7 不是 1,因此您正在打印 (7 * 3) + 1 = 22。然后您将再次调用 testEvenOdd()!因此,它将转到 ifOdd(7),打印出 22,然后再次调用 testEvenOdd()。它很可能会无限重复,直到您耗尽内存。

【讨论】:

    【解决方案3】:

    您按什么顺序调用这些方法?我假设你打电话给start(),然后是testEvenOdd()

    如果输入 7,则在方法 testEvenOdd 中,if((num % 2) == 0) 将为 false(因为 7 % 2 为 1)。所以它会调用方法ifOdd()

    ifOdd 方法中,由于num 为7,if(num == 1) 将为假,它会调用else 子句:num = (num * 3) +1,所以num 将是22 -> 结果(7 * 3) + 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2017-02-14
      相关资源
      最近更新 更多