完整代碼為:
創(chuàng)新互聯建站主要從事網站建設、網站制作、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務贊皇,10年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
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) + "]!");
}
}
運行結果:
普通抽取:
重復時抽?。?/p>
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.util.Random;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?RunningBallDemo?extends?JFrame?{
public?static?void?main(String?args[])?{
new?RunningBallDemo();
}
public?RunningBallDemo()?{
Ball?ballPanel?=?new?Ball(5,?5);
getContentPane().add(ballPanel);
setBackground(Color.BLACK);
addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
setSize(350,?350);
setVisible(true);
Thread?thread1?=?new?Thread(ballPanel);
thread1.start();
}
}
class?Ball?extends?JPanel?implements?Runnable?{
int?rgb?=?0;
Color?color;
int?x,?y;
int?dx?=?5,?dy?=?5;
Ball(int?x,?int?y)?{
this.x?=?x;
this.y?=?y;
}
@Override
protected?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
setBackground(Color.BLACK);
g.setColor(color);
g.fillOval(x,?y,?50,?50);
}
public?void?run()?{
while?(true)?{
if?(x?=?0)?{
dx?=?5;
updateBallColor();
}?else?if?((x?+?50)?=?getWidth())?{
dx?=?-5;
updateBallColor();
}
if?(y?=?0)?{
dy?=?5;
updateBallColor();
}?else?if?((y?+?50)?=?getHeight())?{
dy?=?-5;
updateBallColor();
}
x?=?x?+?dx;
y?=?y?+?dy;
repaint();
try?{
Thread.sleep(25);
}?catch?(InterruptedException?e)?{
;
}
}
}
public?void?updateBallColor()?{
rgb?=?new?Random().nextInt();
color?=?new?Color(rgb);
}
}
//Checkers.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Checkers類
public class Checkers extends JFrame implements ActionListener {
//變量定義
CheckersPanel checkers = new CheckersPanel();
JButton startButton = new JButton("start");
JButton stopButton = new JButton("stop");
//構造函數
public Checkers(){
super("Checkers");
setSize(210,170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
BorderLayout border = new BorderLayout();
pane.setLayout(border);
pane.add(checkers,"Center");
JPanel buttonPanel = new JPanel();
startButton.addActionListener(this);
buttonPanel.add(startButton);
stopButton.addActionListener(this);
stopButton.setEnabled(false);
buttonPanel.add(stopButton);
pane.add(buttonPanel,"South");
setContentPane(pane);
show();
}
//響應用戶動作
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == startButton){
checkers.playAnimation();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}else{
checkers.stopAnimation();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
//主函數
public static void main(String[] arguments){
Checkers ck = new Checkers();
}
}
//CheckersPanel類
class CheckersPanel extends JPanel implements Runnable{
//變量定義
private Thread runner;
int xPos = 5;
int xMove = 4;
//播放動畫
void playAnimation(){
if (runner ==null);{
runner = new Thread(this);
runner.start();
}
}
//停止動畫
void stopAnimation(){
if (runner !=null);{
runner = null;
}
}
//運行
public void run(){
Thread thisThread = Thread.currentThread();
while(runner ==thisThread){
xPos += xMove;
if ((xPos 105)|(xPos 5))
xMove *= -1;
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}
//畫圖形
public void paintComponent(Graphics comp){
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setColor(Color.blue);
comp2D.fillRect(0,0,100,100);
comp2D.setColor(Color.white);
comp2D.fillRect(100,0,100,100);
comp2D.setColor(Color.black);
comp2D.fillOval(xPos,5,90,90);
}
}
import?java.util.Scanner;
/**
*?計算球的體積
*?
*?@author?young
*
*/
public?class?Volume?{
public?static?void?main(String[]?args)?{
System.out.print("請輸入r:");
Scanner?reader?=?new?Scanner(System.in);
double?r?=?0,?v?=?0;
r?=?reader.nextDouble();
v?=?4?*?3.14159?/?3?*?r?*?r?*?r;
System.out.println("球體積為:"?+?String.format("%.2f",?v));
}
}
分享標題:java的小球代碼 java大球吃小球
轉載來源:http://muchs.cn/article22/hggocc.html
成都網站建設公司_創(chuàng)新互聯,為您提供全網營銷推廣、網站設計、外貿建站、定制開發(fā)、品牌網站建設、關鍵詞優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯