java制作跳舞代码 会跳舞的代码源代码
Java数据结构跳舞配对问题(队列的应用)
代码如下,可以直接运行。
在大丰等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、网站设计、外贸网站建设 网站设计制作按需开发网站,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站建设,成都外贸网站制作,大丰网站建设费用合理。
public static void main(String[] args) {
final int M = 6; // number of girls,可改动
final int N = 7; // number of boys,可改动
int x = 3;// some boy,可改动
int y = 5;// some girl,可改动
String result = "";// 记录结果,即第二个问题
// 初始化,假设队列存放男女生编号,从1开始
QueueInteger boys = new LinkedListInteger();
for (int i = 1; i = N; i++) {
boys.add(i);
}
QueueInteger girls = new LinkedListInteger();
for (int i = 1; i = M; i++) {
girls.add(i);
}
// 跳舞开始
int min = boys.size() girls.size() ? girls.size() : boys.size();
int k = 1;// songs
int count = 2; // 求出两个值,可改动
while (k 1000) {//为了不死循环,这里假设最多有999支舞蹈
System.out.println("***This is the " + k + "st dance:");
for (int i = 0; i min; i++) {
// 跳舞,第一个问题:输出每曲配对情况
System.out.println("Boy " + boys.peek() + " = Girl "
+ girls.peek());
// 跳过的排到对尾
int boy = boys.remove();
boys.add(boy);
int girl = girls.remove();
girls.add(girl);
// 判断 x和y跳舞了没有
if (boy == x girl == y) {
result += k + ",";
count--;
}
}
if (count == 0)
break;
// next dance
k++;
}
// 结果
if (count == 0)
System.out.println("\n***Boy " + x + " and Girl " + y
+ " dance together in : " + result);//第二个问题的解答,跳了哪几支舞
else
System.out.println("\n***Boy " + x + " and Girl " + y
+ " have no chance to dance!");//第二个问题的解答,两人没机会跳舞
}
请用java语言设计程序实现:假设舞会上男女生各自一排,每次排头牵手跳舞
6 5 1 2 结果才是6;男排 女排 男序号 女序号
。。。。。。。。。。。。。。。。。。。。。。。。。
package net;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入男排人数:");
int boyArray = sc.nextInt();
System.out.println("请输入女排人数:");
int girlArray = sc.nextInt();
System.out.println("请输入男生序号:");
int boy = sc.nextInt();
System.out.println("请输入女生序号:");
int girl = sc.nextInt();
int count=0;
for(int i=1;i=boyArray*girlArray;i++){
if(iboyArray||igirlArray){
if(i==boyi==girl){
count=i;
System.out.println("该对男女第一次牵手跳舞的序号是a:"+i);
break;
}
}
else if((i%boyArray==boy)(i%girlArray==girl)){
count=i;
System.out.println("该对男女第一次牵手跳舞的序号是:"+i);
break;
}
}
if(count==0){
System.out.println("该对男女不可能一起跳舞!");
}
}
}
数据结构中的舞伴问题。用JAVA实现
/**
假设在周末舞会上,男士们(m人)和女士们(n人)进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。并m和n存在什么条件时,第x个(1 =x =m)男生才有可能和他心仪的第y个(1 =x =n)女生跳舞,在第几首曲子时?
*/
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Dance{
private List String boy=new ArrayList String();
private List String girl=new ArrayList String();
public Dance(int m,int n){
init(m,n);
}
public void init(int m,int n){
for(int i=0;i m;i++){
boy.add("man"+i);
}
for(int i=0;i n;i++){
girl.add("girl"+i);
}
}
public int getMin(){
return Math.min(boy.size(),girl.size());
}
public void go(){
String b="man1";
String g="girl2";
int count=0;
int i=0;
boolean bool=true;
while(bool){
if(i%getMin()==0)
System.out.println("第"+(++count)+"次:");
boolean bGirl=girl.get(i%girl.size()).equals(g);
boolean bBoy=boy.get(i%boy.size()).equals(b);
bool=!(bGirlbBoy);
System.out.println((boy.get((i)%boy.size())).toString());
System.out.println((girl.get((i)%girl.size())).toString());
i++;
}
System.out.println(b+"要在"+count+"首歌中才可以和"+g+"一起Dancing");
}
public static void main(String [] args)throws Exception{
int m;
int n;
System.out.println("请输入男士人数:");
m=input();
System.out.println("请输入女士人数:");
n=input();
Dance dance=new Dance(m,n);
dance.go();
}
public static int input()throws Exception{
int returnNum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=br.readLine();
while(!line.equalsIgnoreCase("exit")){
try{
returnNum=new Integer(line);
if(returnNum 0) throw new Exception();
break;
}catch(Exception e){
System.out.println("输入数据有误!请重新输入,退出请按exit");
line=br.readLine();
}
}
return returnNum;
}
}
求java做动画代码
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TestImage extends Frame
{
private static final long serialVersionUID = 1L;
private static boolean PRESSED = false;
private static int pointX = 0;
private static int pointy = 200;
private static int RIGHT_GO = 0;
private static int LEFT_GO = 0;
private static int DIR = 0;
private static int ANGLE = 0;
private static int W = 50;
private static int H = 60;
private _Canvas canvas = null;
public TestImage ()
{
add (canvas = new _Canvas ());
setIgnoreRepaint (true);
requestFocus ();
}
public class _Canvas extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private BufferedImage bi = null;
private Image bufferedImage = null;
private Thread thread = null;
private long sleepTime = 10;
public _Canvas ()
{
try
{
bi = ImageIO.read (new File ("go.png"));
}
catch (IOException e)
{}
setBackground (Color.BLACK);
requestFocus ();
addKeyListener (new KeyListener ()
{
@Override
public void keyTyped ( KeyEvent e )
{}
@Override
public void keyReleased ( KeyEvent e )
{
RIGHT_GO = 0;
PRESSED = false;
}
@Override
public void keyPressed ( KeyEvent e )
{
// 38 40 37 39上下左右
DIR = e.getKeyCode ();
PRESSED = true;
}
});
}
@Override
public void paint ( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,
this);
g2d.dispose ();
}
@Override
public void update ( Graphics g )
{
if (null == bufferedImage)
{
bufferedImage = createImage (getWidth (), getHeight ());
}
Graphics bufferedG = bufferedImage.getGraphics ();
bufferedG.clearRect (0, 0, getWidth (), getHeight ());
paint (bufferedG);
bufferedG.dispose ();
g.drawImage (bufferedImage, 0, 0, this);
g.dispose ();
}
public void start ()
{
thread = new Thread (this);
thread.setName ("TestImage");
thread.setPriority (Thread.MIN_PRIORITY);
thread.start ();
}
public synchronized void stop ()
{
thread = null;
notify ();
}
@Override
public void run ()
{
Thread me = Thread.currentThread ();
while (thread == me !isShowing () || getSize ().width == 0)
{
try
{
Thread.sleep (555);
}
catch (InterruptedException e)
{
return;
}
}
while (thread == me isShowing ())
{
if (PRESSED)
{
try
{
if (DIR == 39)
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (pointX 420)
{
ANGLE = 90;
pointX--;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 37)
{
pointX = pointX - 1;
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 60;
if (pointX 0)
{
ANGLE = -90;
pointX++;
pointy--;
W = 60;
H = 50;
}
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
}
else if (DIR == 38)
{
W = 50;
H = 60;
pointy = 150;
ANGLE = 0;
RIGHT_GO = 100;
}
else if (DIR == 40)
{
W = 50;
H = 60;
ANGLE = 0;
pointy = 200;
RIGHT_GO = 0;
}
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
else
{
RIGHT_GO = RIGHT_GO + 50;
LEFT_GO = 0;
pointX = pointX + 1;
if (RIGHT_GO 50)
{
RIGHT_GO = 0;
}
if (pointX 500)
{
pointX = 0;
}
try
{
Thread.sleep (sleepTime);
repaint ();
}
catch (InterruptedException e)
{
break;
}
}
}
thread = null;
}
}
/**
* 旋转图像为指定角度
*
* @param degree
* @return
*/
public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )
{
int w = image.getWidth ();
int h = image.getHeight ();
int type = image.getColorModel ().getTransparency ();
BufferedImage img;
Graphics2D graphics2d;
( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);
graphics2d.drawImage (image, 0, 0, null);
graphics2d.dispose ();
return img;
}
public static void main ( String[] args )
{
EventQueue.invokeLater (new Runnable ()
{
@Override
public void run ()
{
final TestImage ti = new TestImage ();
ti.setSize (new Dimension (500, 300));
ti.setLocationRelativeTo (null);
ti.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
@Override
public void windowDeiconified ( WindowEvent e )
{
ti.canvas.start ();
}
@Override
public void windowIconified ( WindowEvent e )
{
ti.canvas.stop ();
}
});
ti.setResizable (false);
ti.canvas.start ();
ti.setVisible (true);
}
});
}
}
当前文章:java制作跳舞代码 会跳舞的代码源代码
URL标题:http://ybzwz.com/article/hhoijc.html