酷站(www.ku0.com)-致力于为互联网从业者提供动力!

热门关键词:  企业  as  baidu  c4rp3nt3r  美女
酷站

js

旗下栏目: php js asp Flex Ajax jquery JSP asp.net C语言 java 正则表达式 微信小程序

java和js实现的洗牌小程序的方法

来源:互联网搜集 作者:秩名 人气: 发布时间:2019-10-02
本篇文章主要介绍了java和js实现的洗牌小程序的方法,对大家的学习或者工作具有一定的参考学习价值,感兴趣的小伙伴们可以参考一下,也感谢大家对酷站(ku0.com)的支持。

这几天刚学了java和javascript,简单写了个用java和javascript的小程序

JavaScript的
 

// An highlighted block
var colors = ['红桃','梅花','方片','黑桃']
var numbers = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
var puke = new Array();
var cards = new Array();
for (var i = 0; i < 52; i++){
  cards[i] = i
}
console.log("洗牌前");
for (var i = 0; i < 52; i++) {
  puke [i] = colors[parseInt(cards[i]/13)]+numbers[cards[i]%13]
 
}
console.log(puke)
console.log("洗牌后")
puke.sort(function () {
  return 0.5-Math.random()
})
console.log(puke)
 

java的

public class Puke{
 public static void main(String[] args) {
 // TODO Auto-generated method stub
    String colors[] = {"黑桃","红桃","梅花","方片"};
    String words[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    String[] cards = new String[52];
    System.out.println("洗牌前:\n");
    for (int i = 0; i < 52; i++) {
  cards[i] = colors[i/13] + words[i%13];
  if (i%13==0) {
  System.out.println();
  }
  System.out.print(" "+cards[i]);
 }
    System.out.println();
    System.out.println("洗牌后");
    String str;
    for(int i=0;i<cards.length;i++){
      int e=(int) Math.round((cards.length-1)*Math.random());
      str=cards[e];
      cards[e]=cards[cards.length-1];
      cards[cards.length-1]=str;
     }
    for (int i = 0; i < 52; i++) { 
  if (i%13==0) {
  System.out.println();
  }
  System.out.print(" "+cards[i]);
 }
 }
}

原文链接:https://blog.csdn.net/qq_43693820/article/details/101604260

最新更新