Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Jquery中使用Java代码使其保持相同的形式_Java_Javascript_Jquery - Fatal编程技术网

如何在Jquery中使用Java代码使其保持相同的形式

如何在Jquery中使用Java代码使其保持相同的形式,java,javascript,jquery,Java,Javascript,Jquery,我已经用Java编写了一段代码,但想在Jquery中使用它,因为我的整个项目现在都在Jquery中工作。如何在Jquery中使用这段代码,确保从数组中获取随机数的功能不会改变。我的代码如下:- import java.util.Random; public class ARRAan { public static void main(String[] args) { int cards[]={1,2,3,6,1,25,24,2,62,85,65,}; in

我已经用Java编写了一段代码,但想在Jquery中使用它,因为我的整个项目现在都在Jquery中工作。如何在Jquery中使用这段代码,确保从数组中获取随机数的功能不会改变。我的代码如下:-

import java.util.Random;
public class ARRAan {
    public static void main(String[] args) {
        int cards[]={1,2,3,6,1,25,24,2,62,85,65,};
        int temp1;
        int temp;
        Random randIndex = new Random();

        for (int i = 0; i <cards.length; i++)
        {
            temp1 = randIndex.nextInt(i+2);
            temp = cards[i];
            cards[i] = cards[temp1];
            cards[temp1] = temp;
            System.out.println(  cards[i] );
        }
    }
}                    
var卡=[1,2,3,6,1,25,24,2,62,85,65];
var temp1;
无功温度;
var randIndex=Math.random();

对于(var i=0;i让我们看看您的问题:

我已经用Java编写了一段代码,但想在Jquery[sic]中使用它

这就是你问题的答案。如果你是用Java编写的,如果你想在基于jQuery的项目中使用它,就必须用JavaScript重写它

这应该不会太难,因为语法是相似的

您只需要重写
main
(其余内容在JS中不相关):

var卡=[1,2,3,6,1,25,24,2,62,85,65];
var temp1;
无功温度;
var randIndex=Math.random();
对于(变量i=0;i
int
替换为
var
,更改数组语法,并将
Random
对象替换为
Math.Random()
调用


这里没什么太棘手的。

你不能在jquery中运行java代码,jquery是一个javascript库,java是一种编程语言。而且jquery更适合UI、动画和其他与web相关的东西,你的代码似乎是逻辑代码。

用javascript重写它。这是一个笑话还是什么……你选择了错误的技术。无论如何,如果您的后端使用JAVA。使用AJAX将请求从jquery发送到jsp或其他东西。@nightgaunt可以吗?您正在用JAVA代码生成随机卡。我不熟悉JAVA或jsp,但应该有一种方法来处理http请求。因此,当您将http请求发送到jsp时,它会将卡发送回jquery。从那里,您可以继续。没有称为jQuery的语言…@Derek朕會功夫 非常正确:已修复。@aspirant没问题。@aspirant有任何不接受的原因吗?您的要求有变化吗?@AstroCB因为被否决了,所以我不这么做:(
function shuffle() {
 return 0.5 - Math.random();
  }
var cards=[1,2,3,6,1,25,24,2,62,85,65];
var temp1;
var temp;
var randIndex = Math.random();

for (var i = 0; i <cards.length; i++)
{
    temp1 = randIndex.nextInt(i+2);
    temp = cards[i];
    cards[i] = cards[temp1];
    cards[temp1] = temp;
    console.log(  cards[i] );
}