2017-08-26 71 views
0

我在努力將Java代碼轉換爲JavaScript。爲此,例如,我將public static int primesolution轉換爲函數primesolution。我不知道我是否正在轉換它的正確軌道。我被困在public static void main(String [] args)。如何將這個函數轉換成Javascript。任何幫助,高度讚賞。如何將Java代碼寫入JavaScript

document.addEventListener("DOMContentLoaded", function(){ 
    // Your main() code here 
}); 

or change it to classic function with parameters like this: 

function mainFunction(yourVar) 

然後,JS:

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); 
    } 

} 

我在JavaScript

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); 
    } 

} 

回答

1

您應該使用文件load事件監聽器(將執行加載HTML後的內容)轉換的代碼是動態類型化的語言,所以你必須使用泛型變量int,long,string或其他。

然後,您可以通過上述功能方面的其他功能方面,所以你最終的JavaScript代碼如下所示:http://esprima.org/demo/validate.html

然後進行調試:

// Use function like this: 
// function yourFunction(parameter1, parameter2, ...) { 
// or use document DOMContentLoaded event listener: 
function yourFunction(a, b, k) { 
    try { 
     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"); 
     } 
     console.log(solution(a, b, k)); 
    } catch(e) { 
     error("Usage: EvaluateDivisors A B K"); 
    } 
}; 

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

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

     var divisors = 3; 

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

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

    var 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(n) { 
    for (var m = 2; m <= Math.sqrt(n); m++) { 
     if (n % m == 0) { 
      return false; 
     } 
    } 
    return true; 
} 

function error(message) { 
    console.log(message); 
} 

我可以使用JavaScript驗證建議您您的瀏覽器的控制檯(在Windows上,你可以在幾乎任何瀏覽器中使用F12打開它)

並搜索上W3Schools的幫助下,它的真棒來源求助: https://www.w3schools.com/js/