java抽取数字代码 java抽奖代码

请问用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:随机抽取100内五个随机数

你要努力啊!这种程序还要来问.这个代码你参考一下吧!多看jdk文档,里面的很多东西包装的很好的.

import java.util.HashSet;

public class Test {

public static void main(String args[]) {

HashSetInteger hs = new HashSetInteger();

while (true) {

int a = (int)(Math.random() * 100);

if(a = 10 a = 100) {

hs.add(a);

}

if (hs.size() == 5) {

break;

}

}

System.out.println(hs);

}

温馨提示:亲 答题不易解题更难 您的支持是我继续答题的动力 麻烦采纳 谢谢

java中如何提取字符串中的数字?

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.Arrays;

public class NumberSplit {

public StringBuffer getString(String str) {

StringBuffer strbuf = new StringBuffer("t");

boolean flag = true;

for (int i = 0; i str.length(); i++) {

char c = str.charAt(i);

// 判断是否是数字

if (c = '0' c = '9') {

// 判断和字符串中的数字是否重复

for (int j = 1; j strbuf.length(); j++) {

if (c == strbuf.charAt(j)) {

// 如果重复,标志位数值为false,并跳出循环;否则标志位设置为true.

flag = false;

break;

} else

flag = true;

}

// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.

if (flag) {

strbuf.append(c);

}

}

}

return strbuf;

}

public static void main(String args[]) throws Exception {

System.out.println("请输入一段字符串,并以回车结束");

BufferedReader buf = new BufferedReader(

new InputStreamReader(System.in));

String str = buf.readLine();

StringBuffer strbuf = new NumberSplit().getString(str);

// 将返回的StringBuffer转换为字符数组

char c[] = strbuf.deleteCharAt(0).toString().toCharArray();

// 对字符数组排序

Arrays.sort(c);

// 输出字符数组

for (int i = 0; i strbuf.length(); i++) {

System.out.print(c[i]);

}

}

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.Arrays;

public class NumberSplit {

public StringBuffer getString(String str) {

StringBuffer strbuf = new StringBuffer("t");

boolean flag = true;

for (int i = 0; i str.length(); i++) {

char c = str.charAt(i);

// 判断是否是数字

if (c = '0' c = '9') {

// 判断和字符串中的数字是否重复

for (int j = 1; j strbuf.length(); j++) {

if (c == strbuf.charAt(j)) {

 // 如果重复,标志位数值为false,并跳出循环;否则标志位设置为true.

flag = false;

 break;

} else

 flag = true;

}

// 只有在即是数字又不重复的情况下才将改字符拼接到字符串上.

if (flag) {

strbuf.append(c);

}

}

}

return strbuf;

}

public static void main(String args[]) throws Exception {

System.out.println("请输入一段字符串,并以回车结束");

BufferedReader buf = new BufferedReader(

new InputStreamReader(System.in));

String str = buf.readLine();

StringBuffer strbuf = new NumberSplit().getString(str);

// 将返回的StringBuffer转换为字符数组

char c[] = strbuf.deleteCharAt(0).toString().toCharArray();

// 对字符数组排序

Arrays.sort(c);

// 输出字符数组

for (int i = 0; i strbuf.length(); i++) {

System.out.print(c[i]);

}

}

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 jsp怎么提取数字

操作方法如下:

使用正则表达式表示匹配除数字以外的字符,然后replaceAll方法使用空字符代替匹配的字符,这样剩下的字符串就只包括数字。最后只需要将str转换为int类型就结束了。

Java是一门面向对象编程语言,1990年代初由詹姆斯·高斯林等人开发出Java语言的雏形,最初被命名为Oak,后随着互联网的发展,经过对Oak的改造,1995年5月Java正式发布。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

java中怎么将字符串中的数字取出来

public class  取数字

{

public static void main(String[] args) 

{

System.out.println("\n\t\t==========将字符串中的数字取出来==========\n");

init();

}//初始化!

private static void init()

{

//分割!

String[] s="今天是2017年09月01日".split("\\D+");

//打印分割的!

for (int i=0;is.length ;i++ )

{

System.out.print(s[i]+" ");

}

System.out.println();

}

}


网站题目:java抽取数字代码 java抽奖代码
本文路径:http://lszwz.com/article/hiepss.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款