十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
完整代碼為:
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的新興網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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("開(kāi)始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球?yàn)椋篬" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重復(fù)抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的紅球?yàn)椋?);
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("\n\n開(kāi)始抽取藍(lán)球!");
System.out.println("本次抽取到的藍(lán)球?yàn)椋篬" + (random.nextInt(16) + 1) + "]!");
}
}
運(yùn)行結(jié)果:
普通抽?。?/p>
重復(fù)時(shí)抽?。?/p>
你要努力啊!這種程序還要來(lái)問(wèn).這個(gè)代碼你參考一下吧!多看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);
}
溫馨提示:親 答題不易解題更難 您的支持是我繼續(xù)答題的動(dòng)力 麻煩采納 謝謝
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);
// 判斷是否是數(shù)字
if (c = '0' c = '9') {
// 判斷和字符串中的數(shù)字是否重復(fù)
for (int j = 1; j strbuf.length(); j++) {
if (c == strbuf.charAt(j)) {
// 如果重復(fù),標(biāo)志位數(shù)值為false,并跳出循環(huán);否則標(biāo)志位設(shè)置為true.
flag = false;
break;
} else
flag = true;
}
// 只有在即是數(shù)字又不重復(fù)的情況下才將改字符拼接到字符串上.
if (flag) {
strbuf.append(c);
}
}
}
return strbuf;
}
public static void main(String args[]) throws Exception {
System.out.println("請(qǐng)輸入一段字符串,并以回車結(jié)束");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = buf.readLine();
StringBuffer strbuf = new NumberSplit().getString(str);
// 將返回的StringBuffer轉(zhuǎn)換為字符數(shù)組
char c[] = strbuf.deleteCharAt(0).toString().toCharArray();
// 對(duì)字符數(shù)組排序
Arrays.sort(c);
// 輸出字符數(shù)組
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);
// 判斷是否是數(shù)字
if (c = '0' c = '9') {
// 判斷和字符串中的數(shù)字是否重復(fù)
for (int j = 1; j strbuf.length(); j++) {
if (c == strbuf.charAt(j)) {
?// 如果重復(fù),標(biāo)志位數(shù)值為false,并跳出循環(huán);否則標(biāo)志位設(shè)置為true.
flag = false;
?break;
} else
?flag = true;
}
// 只有在即是數(shù)字又不重復(fù)的情況下才將改字符拼接到字符串上.
if (flag) {
strbuf.append(c);
}
}
}
return strbuf;
}
public static void main(String args[]) throws Exception {
System.out.println("請(qǐng)輸入一段字符串,并以回車結(jié)束");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = buf.readLine();
StringBuffer strbuf = new NumberSplit().getString(str);
// 將返回的StringBuffer轉(zhuǎn)換為字符數(shù)組
char c[] = strbuf.deleteCharAt(0).toString().toCharArray();
// 對(duì)字符數(shù)組排序
Arrays.sort(c);
// 輸出字符數(shù)組
for (int i = 0; i strbuf.length(); i++) {
System.out.print(c[i]);
}
}
使用正則表達(dá)式可以很方便地從文本中截取數(shù)字,下面是詳細(xì)代碼:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String phoneString = "哈哈,13888889999";
// 提取數(shù)字
// 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("");
}
}
操作方法如下:
使用正則表達(dá)式表示匹配除數(shù)字以外的字符,然后replaceAll方法使用空字符代替匹配的字符,這樣剩下的字符串就只包括數(shù)字。最后只需要將str轉(zhuǎn)換為int類型就結(jié)束了。
Java是一門(mén)面向?qū)ο缶幊陶Z(yǔ)言,1990年代初由詹姆斯·高斯林等人開(kāi)發(fā)出Java語(yǔ)言的雛形,最初被命名為Oak,后隨著互聯(lián)網(wǎng)的發(fā)展,經(jīng)過(guò)對(duì)Oak的改造,1995年5月Java正式發(fā)布。
Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
public?class??取數(shù)字
{
public?static?void?main(String[]?args)?
{
System.out.println("\n\t\t==========將字符串中的數(shù)字取出來(lái)==========\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();
}
}