如何将java代码转换为javascript [英] How to convert java code into javascript

查看:260
本文介绍了如何将java代码转换为javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am struggling to convert Java code into javascript. For that, for example, I am converting public static int primesolution into function primesolution. I do not have much idea whether I am on the right track of converting it. I am stuck with public static void main(String[] args). How to convert this function into Javascript. Any help is highly appreciated.







import java.lang.Math;
public class EvaluateDivisors {

     public static void main(String[] args) {
        try {
            long a = Long.parseLong(args[0]);
            long b = Long.parseLong(args[1]);
            int k = Integer.parseInt(args[2]);

            if (a <= 1 || b <= a) {
                error("Error: must have 1 < A < B");
            }
            if (k <= 0 || k % 2 == 0) {
                error("Error: K must be a positive odd number");
            }
            System.out.println(solution(a, b, k));
        } catch (IndexOutOfBoundsException e) {
            error("Usage: EvaluateDivisors A B K");
        } catch (NumberFormatException e) {
            error("Error: arguments must be integers");
        }
    }


    private static int solution(long a, long b, int k) {


        if (prime(k)) {
            return primeSolution(a, b, k);
        }
        int result = 0;

         for (long n = (long) Math.sqrt(a); n*n <= b; n++) {

           int divisors = 3;


            for (long m = 2; m < n && divisors <= k; m++) {
                if (n*n % m == 0) {
                    divisors += 2;
                }
            }
            if (divisors == k) {
                result++;
            }
        }
        return result;
    }


      private static int primeSolution(long a, long b, int k) {
        int result = 0;


        int n = 2;
        while (Math.pow(n, k - 1) < a) {
            n++;
        }
        while (Math.pow(n, k - 1) <= b) {
            if (prime(n++)) {
                result++;
            }
        }
        return result;
    }


    private static boolean prime(int n) {

        for (int m = 2; m <= Math.sqrt(n); m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }
    private static void error(String message) {
        System.err.println(message);
        System.exit(1);
    }

}





我尝试过: < br $>




What I have tried:

function EvaluateDivisors {

    function main(String[] args) {
        try {
            long a = Long.parseLong(args[0]);
            long b = Long.parseLong(args[1]);
            int k = Integer.parseInt(args[2]);

            if (a <= 1 || b <= a) {
                error("Error: must have 1 < A < B");
            }
            if (k <= 0 || k % 2 == 0) {
                error("Error: K must be a positive odd number");
            }
            System.out.println(solution(a, b, k));
        } catch (IndexOutOfBoundsException e) {
            error("Usage: EvaluateDivisors A B K");
        } catch (NumberFormatException e) {
            error("Error: arguments must be integers");
        }
    }


    function solution(long a, long b, int k) {

        if (prime(k)) {
            return primeSolution(a, b, k);
        }
        int result = 0;

        for (long n = (long) Math.sqrt(a); n*n <= b; n++) {

            int divisors = 3;

            for (long m = 2; m < n && divisors <= k; m++) {
                if (n*n % m == 0) {
                    divisors += 2;
                }
            }
            if (divisors == k) {
                result++;
            }
        }
        return result;
    }

    function primeSolution(long a, long b, int k) {
      int result = 0;

       int n = 2;
      while (Math.pow(n, k - 1) < a) {
          n++;
      }
      while (Math.pow(n, k - 1) <= b) {
          if (prime(n++)) {
              result++;
          }
      }
      return result;
  }


        function prime(int n) {
        for (int m = 2; m <= Math.sqrt(n); m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }

    function error(String message) {
        console.log(message);
        System.exit(1);
    }

}

推荐答案

你没有,因为它不起作用。

这两种语言不能在相同的环境中运行,虽然可以翻译代码本身,但围绕它们的框架却非常非常不同。对于初学者,你展示的Java代码适用于命令行参数,Javascript只是没有。



相反,请坐下来做你的功课问题,并尝试编写自己的代码来完成它设置的任务(这似乎与素数因素有关)。你不会通过copy'n'paste解决方案通过你的课程,因为当你进入期末考试时,你将无法获得任何解决方案来复制'n'paste ...
You don't, because it won't work.
The two languages do not operate in the same environments, and while it may be possible to "translate" the code itself, the framework around them is very, very different. For starters, the Java code you show works on command line parameters, which Javascript just doesn't have.

Instead, sit down with your homework question, and try to write your own code to do the task it sets out (which would appear to be something to do with prime factors). You will not pass your course by copy'n'paste solutions because when you get to the final exam, you won't have any access to solutions to copy'n'paste from...


这篇关于如何将java代码转换为javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆