重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java数字抽取代码 java随机抽取数字

Java随机抽取人名完整代码是什么?

public class test {

为大化等地区用户提供了全套网页设计制作服务,及大化网站建设行业解决方案。主营业务为做网站、网站建设、大化网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

public static void main(String[] args) {

//定义人名数组

String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};

//随机生成数组下标、

int num = (int)(Math.random() * 1000);

//对生成的随机数进行判断,如果小于数组下标,就跳出循环

while (numname.length-1) {

if (num=name.length-1) {

break;

}

num = (int)(Math.random() * 1000);

}

//将数组下标设置成随机数,就可以实现人名的随机抽取

System.out.println(“被抽到的同学是:”+name[num]);

}

}

java中怎样在一个集合中随机取一个数?

你可以获取ArrayList存储这组数据 然后以集合的长度作为随机数的上限,用Random对象的nextInt方法随机取ArrayList对象元素的索引值,利用ArrayList对象的get方法通过索引值获取你需要的数,这样就达到了随机取数的目的

请问用java从1-33个整数中随机抽取6个数字 且不重复 1-16随机抽取一个数,给小球?

完整代码为:

public class Main {

public static void main(String[] args) {

int index = 1;

int[] redBalls = new int[6];

Random random = new Random();

boolean getMoreRed = true;

boolean getAgain;

System.out.println("开始抽取红球!");

while (getMoreRed) {

getAgain = false;

int red = random.nextInt(36) + 1;

System.out.print("本次抽取到的红球为:[" + red + "]!");

for (int i = 0; i index; i++) {

if (redBalls[i] == red) {

System.out.print("重复抽取,将重新抽取红球");

getAgain = true;

break;

}

}

System.out.println("");

if (getAgain){

continue;

}

redBalls[index - 1] = red;

index++;

getMoreRed = index 7;

}

System.out.println("抽取到的红球为:");

Arrays.sort(redBalls);

for (int redBall : redBalls) {

System.out.print(redBall + " ");

}

System.out.println("\n\n开始抽取蓝球!");

System.out.println("本次抽取到的蓝球为:[" + (random.nextInt(16) + 1) + "]!");

}

}

运行结果:

普通抽取:

重复时抽取:

Java如何从字符串中提取数字

使用正则表达式可以很方便地从文本中截取数字,下面是详细代码:

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

String phoneString = "哈哈,13888889999";

// 提取数字

// 1

Pattern pattern = Pattern.compile("[^0-9]");

Matcher matcher = pattern.matcher(phoneString);

String all = matcher.replaceAll("");

System.out.println("phone:" + all);

// 2

Pattern.compile("[^0-9]").matcher(phoneString).replaceAll("");

}

}


标题名称:java数字抽取代码 java随机抽取数字
当前链接:http://cqcxhl.com/article/dddspjp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP