189 8069 5689

java的小球代码 java大球吃小球

请问用java从1-33个整数中随机抽取6个数字 且不重复 1-16随机抽取一个数,给小球?

完整代码为:

创新互联建站主要从事网站建设、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务赞皇,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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) + "]!");

}

}

运行结果:

普通抽取:

重复时抽取:

java小球碰撞窗体边缘来回反弹的代码

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);

}

}

滚动的小球 java源代码

//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);

}

}

关于类的,写一个球的体积的java代码

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大球吃小球
链接URL:http://cdxtjz.cn/article/hggocc.html

其他资讯