2017-08-26 16 views
0

私はJavaコードをJavaScriptに変換するのに苦労しています。そのためには、例えば、私はをpublic static int primesolutionfunction primesolutionに変換しています。私はそれを変換する正しい軌道にいるかどうかについてはあまり考えていません。私はと静かになっています。public static void main(String [] args)。この関数をJavascriptに変換する方法。どんな助けも高く評価されます。JavaScriptにJavaコードを書く方法

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

} 

私はあなたが(それがHTMLをロードした後にその内容を実行します)文書負荷のイベントリスナーを使用する必要があります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

をコードに変換:

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

or change it to classic function with parameters like this: 

function mainFunction(yourVar) 

そして、JSを動的に型付けされた言語なので、int、long、stringなどの汎用var isteadを使用する必要があります。デバッグのためにその後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のバリデータを使用して、あなたをお勧めすることができます:あなたの最後のjavascriptのコードは次のようになりますので

次に、あなたは、上記の機能と並んで他の機能面を配置することができますあなたのブラウザコンソール(WindowsではF12を使用してほぼすべてのブラウザで開くことができます)

w3schoolsのヘルプを検索するには、その素晴らしいヘルプソース: https://www.w3schools.com/js/

関連する問題