請同學們積極提問!
評論
public class RandomTest {
public static void main(String[] args) {
anotherMethod(5);
}
static void oneMethod(int length) {
String s = "";
String s1 = "";
int j = 0;
/**
* 利用字符串來比較是否重復,有重復則重選一個,選幾個,i就小于幾就行了
*/
for (int i = 0; i < length; i++) {
/**
* 調用random方法,隨機選數(shù),它選出的是0-0.999...之間的數(shù),
* 所以乘以length,就是1-length之間的整型數(shù)。
*/
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1) {
i--;
continue;
}
s = s + " " + s1;
//大出來看看,絕對不重,保存下來就行了
System.out.println(j);
}
}
static void anotherMethod(int length) {
int[] a = new int[length];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * length)+1;
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
i--;
break;
}
}
}
for (int j = 0; j < length; j++) {
System.out.println(a[j]);
}
}
static void thirdMethod(int length) {
int numbers[] = new int[length];
/**
* 初始化數(shù)字
*/
for (int k = 0; k < length; k++) {
numbers[k] = k;
}
int random[] = new int[length];
/**
* 比如ranInt(54)產(chǎn)生一個隨機數(shù)5,把a[5]的牌抽出數(shù)組,
* 然后再用ranInt(53)抽出另外一張,ranInt(52)
*/
for (int i = length - 1; i >= 0; i--) {
int selected = (int) (Math.random() * i)+1;
random[i] = numbers[selected];
numbers[selected] = numbers[i];
}
for (int i = 0; i < length; i++) {
System.out.print(random[i]);
}
}
static int genRandomCut(int length) {
int randomnum = (int) (Math.random() * length);
//System.out.print(randomnum);
return randomnum ;
}
static int[] getRandom(int length){
int[] a = new int[length];
loop:
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * length);
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
i--;
continue loop;
}
}
}
for (int j = 0; j < length; j++) {
System.out.println(a[j]);
}
return a;
}
static void cutCard(int length) {
int[] cards = new int[length];
cards = getRandom(length);
int randomcut = genRandomCut(length);
int temp[] = new int[length];
int startPoint = randomcut;
for(int i=0; i<length-randomcut; i++){
temp[i]=cards[startPoint++];
}
for(int k=0; k<randomcut; k++){
temp[length-randomcut+k] = cards[k];
}
System.out.println("RandomCut:"+randomcut);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
} 回復 更多評論
public static void main(String[] args) {
anotherMethod(5);
}
static void oneMethod(int length) {
String s = "";
String s1 = "";
int j = 0;
/**
* 利用字符串來比較是否重復,有重復則重選一個,選幾個,i就小于幾就行了
*/
for (int i = 0; i < length; i++) {
/**
* 調用random方法,隨機選數(shù),它選出的是0-0.999...之間的數(shù),
* 所以乘以length,就是1-length之間的整型數(shù)。
*/
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1) {
i--;
continue;
}
s = s + " " + s1;
//大出來看看,絕對不重,保存下來就行了
System.out.println(j);
}
}
static void anotherMethod(int length) {
int[] a = new int[length];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * length)+1;
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
i--;
break;
}
}
}
for (int j = 0; j < length; j++) {
System.out.println(a[j]);
}
}
static void thirdMethod(int length) {
int numbers[] = new int[length];
/**
* 初始化數(shù)字
*/
for (int k = 0; k < length; k++) {
numbers[k] = k;
}
int random[] = new int[length];
/**
* 比如ranInt(54)產(chǎn)生一個隨機數(shù)5,把a[5]的牌抽出數(shù)組,
* 然后再用ranInt(53)抽出另外一張,ranInt(52)
*/
for (int i = length - 1; i >= 0; i--) {
int selected = (int) (Math.random() * i)+1;
random[i] = numbers[selected];
numbers[selected] = numbers[i];
}
for (int i = 0; i < length; i++) {
System.out.print(random[i]);
}
}
static int genRandomCut(int length) {
int randomnum = (int) (Math.random() * length);
//System.out.print(randomnum);
return randomnum ;
}
static int[] getRandom(int length){
int[] a = new int[length];
loop:
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * length);
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
i--;
continue loop;
}
}
}
for (int j = 0; j < length; j++) {
System.out.println(a[j]);
}
return a;
}
static void cutCard(int length) {
int[] cards = new int[length];
cards = getRandom(length);
int randomcut = genRandomCut(length);
int temp[] = new int[length];
int startPoint = randomcut;
for(int i=0; i<length-randomcut; i++){
temp[i]=cards[startPoint++];
}
for(int k=0; k<randomcut; k++){
temp[length-randomcut+k] = cards[k];
}
System.out.println("RandomCut:"+randomcut);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
} 回復 更多評論
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Random;
class Card{
private String value; //撲克牌的值
private String color; //撲克牌的花色
public Card(String s1,String s2){
.....
}
public String GetValue(){
.....
}
public String GetColor(){
.....
}
}//一張牌的類型
我已經(jīng)建立好了一個Card[]Porke=new Card[54];且對其進行了初始化及相關賦值,即建立好了一副牌.
主要問題是出在洗牌的時候.部分代碼如下:
List cards = new ArrayList();
//向其中添加元素;
for(int i = 0; i < 54; i++) {
cards.add(Porke[i]); //即將Porke中各張牌給這個list中
}
Collections.shuffle(cards,new Random()); //將cards中元素順序打亂
建立一個新的Card newcard[54];
newcard=cards.toArray(); //將list cards轉化成對應元素類型的數(shù)組
可是在執(zhí)行的時候就是這個地方出錯.錯誤是: 類型不一致
回復 更多評論
import java.util.Collections;
import java.util.ArrayList;
import java.util.Random;
class Card{
private String value; //撲克牌的值
private String color; //撲克牌的花色
public Card(String s1,String s2){
.....
}
public String GetValue(){
.....
}
public String GetColor(){
.....
}
}//一張牌的類型
我已經(jīng)建立好了一個Card[]Porke=new Card[54];且對其進行了初始化及相關賦值,即建立好了一副牌.
主要問題是出在洗牌的時候.部分代碼如下:
List cards = new ArrayList();
//向其中添加元素;
for(int i = 0; i < 54; i++) {
cards.add(Porke[i]); //即將Porke中各張牌給這個list中
}
Collections.shuffle(cards,new Random()); //將cards中元素順序打亂
建立一個新的Card newcard[54];
newcard=cards.toArray(); //將list cards轉化成對應元素類型的數(shù)組
可是在執(zhí)行的時候就是這個地方出錯.錯誤是: 類型不一致
回復 更多評論
名稱: 實現(xiàn)洗牌算法
分析:
識別類:
public class Card{}; //一張牌
public class Poker{}; //一副牌
public class PlayGame{}; //對牌進行的相關操作,包括洗牌,切牌
識別屬性:
Card應包括的屬性有: private color(花色),private value(大小);
Poker應包括的屬性有:Card Cards[54]; //包含54張牌元素的數(shù)組,用來描述一副牌;
public String color[4]; //包含4種花色
public String value[13]={"A","2","3",......,"K"}
定義類:
Card{
屬性: private color //花色
private value //值,大小
構造函數(shù):
根據(jù)需要,牌大致可劃分為兩種,一種是A,2....K.(普通牌),另外一種就是大王,小王
Card(String value,String color){} //用來初始化普通牌
Card(String value){} //傳入一個參數(shù),用來初始化大王,小王
方法:
為了得到private的屬性,定義以下方法來獲取
String getColor(){}; //得到這張牌的花色
String getValue(){}; //用來得到這張牌子的大小;
}
Poker{
屬性: 因為每副牌都有共同屬性:四種花色,有從A,2....K十三種值類型,每副牌都有54張,故有以下屬性
public String color[4]={.....}
public String Value[13]={.....}
Card Cards[54];
構造函數(shù):
根據(jù)以上的屬性,分析可知一副牌初始化時不需要傳參,
Porke{
......
}
方法:
每副牌都應該要有一個顯示的方法,打印出Cards[54]的內容.如下:
public void print(){}
}
PlayGame{
分析可知,在該類中我們需要做的是定義一個洗牌的方法,切牌的方法,還可包括main函數(shù)。
無需構造函數(shù),
方法:
洗牌方法: public Poker XiPai(){}
洗牌算法描述:
用random產(chǎn)生一組隨機且不重復的數(shù),用int array[54] 來保存,其中每一個元素都對應 著Cards[54]的下標,
將Cards[54]以該int array[54]的值的順序來存放,則是一組隨機的Card元素,從而實現(xiàn)了洗 牌算法
最后返回值為新的序列的Cards[54]
切牌方法:
public Poker DaoPai(){}
切牌算法描述:
用random產(chǎn)生一個隨機數(shù),找到該隨機數(shù)為下標的Cards[54]的元素。將該元素以前的元素 接到該Cards[54]數(shù)組的后面。最后返回一個新的序列的Cards[54]。從而實現(xiàn)倒牌算法
main函數(shù):
通過對Poker new一個新的對象,來進行洗牌,倒牌的操作
}
回復 更多評論
分析:
識別類:
public class Card{}; //一張牌
public class Poker{}; //一副牌
public class PlayGame{}; //對牌進行的相關操作,包括洗牌,切牌
識別屬性:
Card應包括的屬性有: private color(花色),private value(大小);
Poker應包括的屬性有:Card Cards[54]; //包含54張牌元素的數(shù)組,用來描述一副牌;
public String color[4]; //包含4種花色
public String value[13]={"A","2","3",......,"K"}
定義類:
Card{
屬性: private color //花色
private value //值,大小
構造函數(shù):
根據(jù)需要,牌大致可劃分為兩種,一種是A,2....K.(普通牌),另外一種就是大王,小王
Card(String value,String color){} //用來初始化普通牌
Card(String value){} //傳入一個參數(shù),用來初始化大王,小王
方法:
為了得到private的屬性,定義以下方法來獲取
String getColor(){}; //得到這張牌的花色
String getValue(){}; //用來得到這張牌子的大小;
}
Poker{
屬性: 因為每副牌都有共同屬性:四種花色,有從A,2....K十三種值類型,每副牌都有54張,故有以下屬性
public String color[4]={.....}
public String Value[13]={.....}
Card Cards[54];
構造函數(shù):
根據(jù)以上的屬性,分析可知一副牌初始化時不需要傳參,
Porke{
......
}
方法:
每副牌都應該要有一個顯示的方法,打印出Cards[54]的內容.如下:
public void print(){}
}
PlayGame{
分析可知,在該類中我們需要做的是定義一個洗牌的方法,切牌的方法,還可包括main函數(shù)。
無需構造函數(shù),
方法:
洗牌方法: public Poker XiPai(){}
洗牌算法描述:
用random產(chǎn)生一組隨機且不重復的數(shù),用int array[54] 來保存,其中每一個元素都對應 著Cards[54]的下標,
將Cards[54]以該int array[54]的值的順序來存放,則是一組隨機的Card元素,從而實現(xiàn)了洗 牌算法
最后返回值為新的序列的Cards[54]
切牌方法:
public Poker DaoPai(){}
切牌算法描述:
用random產(chǎn)生一個隨機數(shù),找到該隨機數(shù)為下標的Cards[54]的元素。將該元素以前的元素 接到該Cards[54]數(shù)組的后面。最后返回一個新的序列的Cards[54]。從而實現(xiàn)倒牌算法
main函數(shù):
通過對Poker new一個新的對象,來進行洗牌,倒牌的操作
}
回復 更多評論
我的方法也可以得到結果,代碼也少,但是感覺我的方法不好,我基礎差不知道不好在那些方面
public class RandomTest {
static void oneMethod(int length)
{
String s = "";
String s1 = "";
int j = 0;
for (int i = 0; i < length; i++)
{
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1)
{
i--;
continue;
}
s = s + " " + s1;
String a[]={"黑2","紅2","方快2","梅花2","方三",} //太長老要寫54個暫時寫5個
System.out.println(a[j]);
}
}
public static void main(String[] args){
RandomTest M=new RandomTest() ;
;
M. oneMethod(5);
}
}
回復 更多評論
public class RandomTest {
static void oneMethod(int length)
{
String s = "";
String s1 = "";
int j = 0;
for (int i = 0; i < length; i++)
{
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1)
{
i--;
continue;
}
s = s + " " + s1;
String a[]={"黑2","紅2","方快2","梅花2","方三",} //太長老要寫54個暫時寫5個
System.out.println(a[j]);
}
}
public static void main(String[] args){
RandomTest M=new RandomTest() ;
;
M. oneMethod(5);
}
}
回復 更多評論
我的方法也可以得到結果,代碼也少,但是感覺我的方法不好,我基礎差不知道不好在那些方面
public class RandomTest {
static void oneMethod(int length)
{
String s = "";
String s1 = "";
int j = 0;
for (int i = 0; i < length; i++)
{
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1)
{
i--;
continue;
}
s = s + " " + s1;
String a[]={"黑2","紅2","方快2","梅花2","方三",} //太長老要寫54個暫時寫5個
System.out.println(a[j]);
}
}
public static void main(String[] args){
RandomTest M=new RandomTest() ;
;
M. oneMethod(5);
}
}
回復 更多評論
public class RandomTest {
static void oneMethod(int length)
{
String s = "";
String s1 = "";
int j = 0;
for (int i = 0; i < length; i++)
{
j = (int) (Math.random() * length);
s1 = j + "";
if (s.indexOf(s1) != -1)
{
i--;
continue;
}
s = s + " " + s1;
String a[]={"黑2","紅2","方快2","梅花2","方三",} //太長老要寫54個暫時寫5個
System.out.println(a[j]);
}
}
public static void main(String[] args){
RandomTest M=new RandomTest() ;
;
M. oneMethod(5);
}
}
回復 更多評論
要實現(xiàn)小球在往返彈動效果。。請問怎么修改,
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class GameFrame {
public GameFrame() {
Frame app = new Frame("GameFrame");
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
drawBall drawB = new drawBall();
app.add(drawB, BorderLayout.CENTER);
app.pack();
app.setVisible(true);
app.setLocationRelativeTo(null);
drawB.gameLoop();
}
public class drawBall extends JPanel implements Runnable {
private int x,dx;
private int y,dy;
private int diameter;
private int width;
private int heigth;
private Image im;
private Graphics dbg;
private Thread gamethread;
private static final int FPS=100;
public drawBall() {
x=100;y=100;
dx=0;dy=0;// 球的移動量
diameter=100;//半徑
width=500;
heigth=500;
setBackground(Color.pink);
setPreferredSize(new Dimension(width, heigth));
}
public void gameStart(){
gamethread = new Thread(this);
gamethread.start();
}
public void gameLoop(){
while(true){
gameUpdate();
gameRender();
gamePaint();
}
}
public void gamePaint() {
Graphics g;
try {
g = this.getGraphics();
if (g != null && im != null) {
g.drawImage(im, 0, 0, null);
}
g.dispose();
} catch (Exception e) {
}
}
public void gameRender() {
if (im == null) {
im = createImage(width, heigth);
if (im == null) {
System.out.println("im is null");
} else {
dbg = im.getGraphics();
}
}
dbg.setColor(Color.pink);
dbg.fillRect(0, 0, width, heigth);
dbg.setColor(Color.blue);
dbg.fillOval(x, y, diameter, diameter);
}
public void gameUpdate() {
Random random = new Random();
switch (random.nextInt(4)) {
case 0:
x +=5;
break;
case 1:
x -=5;
break;
case 2:
y +=5;
break;
case 3:
y -=5;
break;
}
//x += dx; y += dy;
if(x>=width-diameter||x-diameter<=0) {
y+=dy;
x-=dx;
dx=(-dx);
}// 超出左右邊界
if(dx>0){ x=x+dx+randomNum; dy=(-dy+randomNum); }
else { x=x+dx-randomNum; dy=(-dy-randomNum); }
else if (y>=heigth-diameter||y-diameter<=0){
y-=dy;
x+=dx;
dy=(-dy);
}// 超出上下邊界
}
public void run() {
long beforeTime,timeDiff,sleepTime;
long period=1000/FPS;
beforeTime = System.currentTimeMillis();
while(true){
gameUpdate();
gameRender();
gamePaint();
timeDiff=(System.currentTimeMillis() - beforeTime)/1000000L;
sleepTime = period - timeDiff;
if(sleepTime<=0)
sleepTime=2;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) { }
beforeTime=System.currentTimeMillis();
System.out.println("sleepTime is:"+sleepTime);
}
}
}
public static void main(String[] args) {
new GameFrame();
}
}
回復 更多評論
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class GameFrame {
public GameFrame() {
Frame app = new Frame("GameFrame");
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
drawBall drawB = new drawBall();
app.add(drawB, BorderLayout.CENTER);
app.pack();
app.setVisible(true);
app.setLocationRelativeTo(null);
drawB.gameLoop();
}
public class drawBall extends JPanel implements Runnable {
private int x,dx;
private int y,dy;
private int diameter;
private int width;
private int heigth;
private Image im;
private Graphics dbg;
private Thread gamethread;
private static final int FPS=100;
public drawBall() {
x=100;y=100;
dx=0;dy=0;// 球的移動量
diameter=100;//半徑
width=500;
heigth=500;
setBackground(Color.pink);
setPreferredSize(new Dimension(width, heigth));
}
public void gameStart(){
gamethread = new Thread(this);
gamethread.start();
}
public void gameLoop(){
while(true){
gameUpdate();
gameRender();
gamePaint();
}
}
public void gamePaint() {
Graphics g;
try {
g = this.getGraphics();
if (g != null && im != null) {
g.drawImage(im, 0, 0, null);
}
g.dispose();
} catch (Exception e) {
}
}
public void gameRender() {
if (im == null) {
im = createImage(width, heigth);
if (im == null) {
System.out.println("im is null");
} else {
dbg = im.getGraphics();
}
}
dbg.setColor(Color.pink);
dbg.fillRect(0, 0, width, heigth);
dbg.setColor(Color.blue);
dbg.fillOval(x, y, diameter, diameter);
}
public void gameUpdate() {
Random random = new Random();
switch (random.nextInt(4)) {
case 0:
x +=5;
break;
case 1:
x -=5;
break;
case 2:
y +=5;
break;
case 3:
y -=5;
break;
}
//x += dx; y += dy;
if(x>=width-diameter||x-diameter<=0) {
y+=dy;
x-=dx;
dx=(-dx);
}// 超出左右邊界
if(dx>0){ x=x+dx+randomNum; dy=(-dy+randomNum); }
else { x=x+dx-randomNum; dy=(-dy-randomNum); }
else if (y>=heigth-diameter||y-diameter<=0){
y-=dy;
x+=dx;
dy=(-dy);
}// 超出上下邊界
}
public void run() {
long beforeTime,timeDiff,sleepTime;
long period=1000/FPS;
beforeTime = System.currentTimeMillis();
while(true){
gameUpdate();
gameRender();
gamePaint();
timeDiff=(System.currentTimeMillis() - beforeTime)/1000000L;
sleepTime = period - timeDiff;
if(sleepTime<=0)
sleepTime=2;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) { }
beforeTime=System.currentTimeMillis();
System.out.println("sleepTime is:"+sleepTime);
}
}
}
public static void main(String[] args) {
new GameFrame();
}
}
回復 更多評論
只有注冊用戶登錄后才能發(fā)表評論。 | ||
![]() |
||
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||