完整代碼為:
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)贊皇,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):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("本次抽取到的紅球?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開始抽取藍(lán)球!");
System.out.println("本次抽取到的藍(lán)球?yàn)椋篬" + (random.nextInt(16) + 1) + "]!");
}
}
運(yùn)行結(jié)果:
普通抽取:
重復(fù)時(shí)抽?。?/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");
//構(gòu)造函數(shù)
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();
}
//響應(yīng)用戶動(dòng)作
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);
}
}
//主函數(shù)
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;
//播放動(dòng)畫
void playAnimation(){
if (runner ==null);{
runner = new Thread(this);
runner.start();
}
}
//停止動(dòng)畫
void stopAnimation(){
if (runner !=null);{
runner = null;
}
}
//運(yùn)行
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;
/**
*?計(jì)算球的體積
*?
*?@author?young
*
*/
public?class?Volume?{
public?static?void?main(String[]?args)?{
System.out.print("請(qǐng)輸入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));
}
}