寻求Java高手,用数组实现随机输出1,2,3,4,5,6,7,8,9,10,J,Q,K中的四个数
6个回答
展开全部
public static void main(String[] args) {
String[] a = {"1","2","3","4","5","6","7","8","9","10","j","q","k"};
String[] b = new String[4];
int n = a.length;
for(int i = 0; i < b.length; i++) {
int r = (int)(Math.random()*n);
b[i] = a[r];
a[r] = a[n - 1];
n--;
System.out.print(b[i]+" ");
}
}
//你也可以自己改成用户输入。
String[] a = {"1","2","3","4","5","6","7","8","9","10","j","q","k"};
String[] b = new String[4];
int n = a.length;
for(int i = 0; i < b.length; i++) {
int r = (int)(Math.random()*n);
b[i] = a[r];
a[r] = a[n - 1];
n--;
System.out.print(b[i]+" ");
}
}
//你也可以自己改成用户输入。
展开全部
假设数组是a[];
import java.util.Random;
Random random = new Random();
for(int x = 0; x<4; x++){
System.out.println(a[random.nextInt(13)]);
}
需要不重复么?
import java.util.Random;
Random random = new Random();
for(int x = 0; x<4; x++){
System.out.println(a[random.nextInt(13)]);
}
需要不重复么?
追问
输出的全是0。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List date = new List(13);
date.add("1");
date.add("2");
.......
date.add("K");
Random r = new Random();
for(int i=0;i<13;i++){
//允许重复
String result = (String)date.get(r.nextInt(date.getSize()));//result 即为随机值。
//不允许重复,请加一下
// date.remove(result);
}
date.add("1");
date.add("2");
.......
date.add("K");
Random r = new Random();
for(int i=0;i<13;i++){
//允许重复
String result = (String)date.get(r.nextInt(date.getSize()));//result 即为随机值。
//不允许重复,请加一下
// date.remove(result);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Random;
public class random {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
while(i++<70)
{
Random rd=new Random();
int n=rd.nextInt(13);
if(n<=10 && n>0)
System.out.println(n);
else
switch(n)
{
case 11:
System.out.println('J');
break;
case 12:
System.out.println('Q');
break;
case 13:
System.out.println('k');
break;
}
}
}
}
public class random {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
while(i++<70)
{
Random rd=new Random();
int n=rd.nextInt(13);
if(n<=10 && n>0)
System.out.println(n);
else
switch(n)
{
case 11:
System.out.println('J');
break;
case 12:
System.out.println('Q');
break;
case 13:
System.out.println('k');
break;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class TestMathrandom {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
int ran;
String[] array = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
System.out.println(array.length);
do {
ran = (int) (Math.random() * 100 / 7);
if (ran >= array.length)
continue;
System.out.print(array[ran] + " ");
} while (i++ <3);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
int ran;
String[] array = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
System.out.println(array.length);
do {
ran = (int) (Math.random() * 100 / 7);
if (ran >= array.length)
continue;
System.out.print(array[ran] + " ");
} while (i++ <3);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先建一个文本文件取名为A.java.
public class A {
public static void main(String[] args)//java的固定模式
{
int i = 0;//初始化i的值
int ran;
String[] array = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };//定义数组并且赋值
System.out.println(array.length);
do {
ran = (int) (Math.random() * 100 / 7);//使用指定的种子值初始化 Random 类
if (ran >= array.length)
continue;
System.out.print(array[ran] + " ");
} while (i++ <3);
}
}
public class A {
public static void main(String[] args)//java的固定模式
{
int i = 0;//初始化i的值
int ran;
String[] array = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };//定义数组并且赋值
System.out.println(array.length);
do {
ran = (int) (Math.random() * 100 / 7);//使用指定的种子值初始化 Random 类
if (ran >= array.length)
continue;
System.out.print(array[ran] + " ");
} while (i++ <3);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询