简体   繁体   English

我在以下 Java 代码中遇到错误,任何人都可以帮助我

[英]I am facing an error in the following java code can anyone help me out

class Solution {
  
    long  maxGcd(int  N) {
        long  a = (N * (N - 1))/ gcd(N,N-1);
        long  b = ((N-2)*(N-3))/ gcd(N-2,N-3);
        long  c = (a*b)/ gcd(a,b);
        long  num = N*(N-1);
        int count = 0;
        for (int i = N - 2; i >= 1; i--) {
            if (gcd(num, i) == 1) {
                num *= i;
                count++;
            }
            if (count == 2) break;
        }
        return  Math.max(c, num);
    }

    long  gcd(long x, long y)
    {
        if(y==0)
        return x;
        return gcd(y,x*y);
    }
}

Error:错误:

Exception in thread "main" java.lang.ArithmeticException: / by zero线程“main”中的异常 java.lang.ArithmeticException: / 为零

I think I am getting this error because if, at some point, b=0 then c will be undefined.我想我收到这个错误是因为如果在某个时候 b=0 那么 c 将是未定义的。 I tried changing it to我试着把它改成

if(b!=0){
    long c =(a*b)/gcd(a,b)

but after running it, gives an error saying但是运行之后报错

prog.java:43: error: cannot find symbol
        return  Math.max(c, num);

Can anyone help me with the solution of this error?谁能帮我解决这个错误?

Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N.给定一个整数 N。找到可以从四个小于或等于 N 的数字中获得的最大 LCM(最小公倍数)。
Note: Duplicate numbers can be used.注意:可以使用重复的数字。

Example 1:示例 1:

Input:输入:
N = 4 N = 4
Output: 12输出:12
Explanation:解释:
The four numbers can be [4,4,3,2] or [4,4,4,3], etc. It can be shown that 12 is the maximum LCM of four numbers that can be obtained from numbers less than or equal to 4.四个数可以是[4,4,3,2]或[4,4,4,3]等。可以证明12是小于等于数可得到的四个数的最大LCM到 4。

I have tried the above problem but it was giving me unexpected error.我已经尝试过上述问题,但它给了我意想不到的错误。

If I'm getting this right, it looks like there is a problem with your function "gcd": the recursive loop will never end如果我做对了,看起来你的函数“gcd”有问题:递归循环永远不会结束

If you divide some digit by 0 it will throw an error known as: java.lang.ArithmeticException如果你将某个数字除以 0,它会抛出一个错误,称为: java.lang.ArithmeticException

Solution解决方案

  1. try, catch block尝试,捕捉块
  2. in catch block you can show msg to user ("Number cannot be divided by 0")在 catch 块中,您可以向用户显示消息(“数字不能被 0 除”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码遇到了运行时错误,有人可以帮我找出我哪里错了 - i got Runtime Error for my code ,can anyone help me to find out where i am wrong 任何人都可以帮助我了解在 Android 中使用 AsyncTask 时出现错误代码 400 - Can anyone help me about I am getting error code 400 while working with AsyncTask in Android 我的代码中遇到了一些问题。 一切运行良好,直到用户输入是一个字符串。 谁能帮我处理异常? - I am facing some issue in my code. Everything runs fine until the user input is a string. Can anyone help me with the exception handling? 谁能帮我解决这个Java错误? - Can anyone help me with this java error ??? 有人可以帮助我编写我正在编写的代码吗? (爪哇) - Can someone please help me with the code I am writing? (Java) 你能帮我找出这个 Java 代码中的错误吗 - Can you help me to find out the error in this Java Code 来自孩子 class 的测试用例没有运行,遇到错误谁能指导我哪里出错了 - Test cases from child class is not running, facing error can anyone please guide me where I am going wrong 我从Firebase获取数据并在TextView中显示它但我收到了一些错误。 任何人都可以检查我的代码并建议我 - I am getting data from Firebase and showing it in the TextView but I am getting some error. Can anyone please check out my code and suggest me 谁能帮助我修改Java代码? - Can anyone help me modify my code in Java? 谁能帮助我使此Java运行无误? - Can anyone help me to make this java run without error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM