【问题标题】:Error says it's dividing by zero while it's not initialised as zero错误说它被零除,而它没有被初始化为零
【发布时间】:2019-10-29 10:01:10
【问题描述】:

我试图找出这个数字是否是自同构的,代码编译得很好,但它说在我标记它的那一行被零除

import java.util.Scanner;
class AutoLoop
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to check if it's automorphic or not");
        int num = sc.nextInt();
        int sqr = num*num;
        int sqrSave=sqr;
        int divisor = 10;
        int counter = 1;
        while (counter<=100)
        {
            sqr = sqr%divisor;  //It says there is divide by zero error on this line
            if (sqr==num)
            {
                System.out.println("Number is an automorphic number");
                break;
            }
            sqr=sqrSave;
            divisor*=10;
            counter++;
            if (counter == 100)
                 System.out.println("Number is not an automorphic number till the 100th digit.");
        }
    }
}

我已经尝试过让它 sqr%=divisor;还是不行。

【问题讨论】:

  • 您不能将 100 位数字放入 int。所以它会溢出,可能会变成0。
  • num的值是多少?
  • num 是用户输入的数字。当我使用小到 2 的数字时,它还说除以零错误。似乎只要有任何数字输入而不是自同构数字,它就会给出错误。
  • 似乎无关,但我不认为您的课程是公开的。您如何真正能够执行 main 方法?主函数也没有 String[] 参数。你能分享完整的例子吗
  • Umm ...您使用double 的版本仅在不引发异常的意义上起作用。随着divisor 变大,它将失去较低数字的准确性。由于较低的数字在计算 sqr 时至关重要,因此您很可能会得到错误的答案。

标签: java bluej


【解决方案1】:

我已经测试了您的代码,似乎您尝试将除数变量乘以超过 32 位整数限制,由于这是不可能的,它变成了一些其他数字,一段时间后它变成了零。每次输入非自守数字时都会发生这种情况

这是每次代码循环时除数的值:

10

100

1000

10000

100000

1000000

10000000

100000000

1000000000

1410065408

1215752192

-727379968

1316134912

276447232

-1530494976

1874919424

1569325056

-1486618624

-1981284352

1661992960

-559939584

-1304428544

-159383552

-1593835520

1241513984

-469762048

-402653184

268435456

-1610612736

1073741824

-2147483648

0

解决此问题的方法可能是将除数设为双精度或浮点数,而不是整数

【讨论】:

    【解决方案2】:

    最初除数是 10。然后循环使除数乘以 10 一百奇数次,得到 10101。这会多次溢出 int 范围,因为 231 大约是 2*1030。

    特别是101000 = 2100*5100,所以循环32次后,每次乘以2,所有位divisor 为零。

    如果通过溢出divisor 变为 0,您会得到这样的除以零错误。由于模 % 抽象地暗示了内部除法(模是除法的余数)。

    【讨论】:

      【解决方案3】:
      import java.util.Scanner;
      class AutoLoop{
          public static void main(){
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter a number to check if it's     automorphic or not");
              int num = sc.nextInt();
              int sqr = num*num;
              int sqrSave=sqr;
              int divisor = 10;
              int counter = 1;
              /* try with 10 and see the result. 100 is too big*/
              while (counter<=100){
                  sqr = sqr%divisor;
                  if (sqr==num){
                      System.out.println("Number is an automorphic number");
                      break;
                  }
                  sqr=sqrSave;
                  divisor*=10;
                  counter++;
                  // try with 10 or an other number smaller than 100
                  if (counter == 100)System.out.println("Number is not an automorphic number till the 100th digit.");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多