跳舞java动态代码,java动态执行代码
请用java语言设计程序实现:假设舞会上男女生各自一排,每次排头牵手跳舞
6 5 1 2 结果才是6;男排 女排 男序号 女序号
创新互联建站-专业网站定制、快速模板网站建设、高性价比祁门网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式祁门网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖祁门地区。费用合理售后完善,十载实体公司更值得信赖。
。。。。。。。。。。。。。。。。。。。。。。。。。
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数据结构跳舞配对问题(队列的应用)
代码如下,可以直接运行。
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实现
/**
假设在周末舞会上,男士们(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的编程题~~!!
regedit;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
public class Regedit extends JFrame {
private JLabel account = new JLabel("账号");
private JTextField aText = new JTextField(10);
private JLabel password = new JLabel("密码");
private JPasswordField pText = new JPasswordField(10);
private JLabel conpass = new JLabel("确认密码");
private JPasswordField cText = new JPasswordField(10);
private JLabel birthday = new JLabel(" 生日");
private JComboBox year = new JComboBox(data(11, 1985));
private JLabel y = new JLabel("年");
private JComboBox month = new JComboBox(data(13, 0));
private JLabel m = new JLabel("月");
private JComboBox day = new JComboBox(data(32, 0));
private JLabel d = new JLabel("日");
private JLabel sex = new JLabel("性别");
private ButtonGroup group = new ButtonGroup();
private JRadioButton man = new JRadioButton("男", false);
private JRadioButton woman = new JRadioButton("女", false);
private JLabel interest = new JLabel(" 兴趣");
private JCheckBox read = new JCheckBox("阅读");
private JCheckBox sing = new JCheckBox("唱歌");
private JCheckBox dance = new JCheckBox("跳舞");
private JLabel remakes = new JLabel(" 备注");
private JTextArea rText = new JTextArea(6, 15);
private JButton button = new JButton("注册");
private String name, pass1, pass2, birth = "", intre = "", se, remake;
public static void main(String[] args) {
new Regedit();
}
public Regedit() {
setTitle("Regedit");
JPanel tz = new JPanel(new GridLayout(1, 2));
JPanel tOne = new JPanel(new GridLayout(3, 2));
JPanel tTwo = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel tThree = new JPanel(new BorderLayout());
JPanel tFour = new JPanel(new BorderLayout());
JPanel tFive = new JPanel(new GridLayout(3, 1));
JPanel tSix = new JPanel(new GridLayout(1, 2));
JPanel tSeven = new JPanel(new GridLayout(2, 1));
JPanel tEight = new JPanel(new GridLayout(2, 1));
JPanel tNine = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel tTen = new JPanel(new GridLayout(1, 2));
setLayout(new GridLayout(1, 2));
tOne.add(account);
tOne.add(aText);
aText.addActionListener(new Textaction(aText));
tOne.add(password);
tOne.add(pText);
pText.addActionListener(new Textaction(pText));
pText.setEchoChar('*');
tOne.add(conpass);
tOne.add(cText);
cText.addActionListener(new Textaction(cText));
cText.setEchoChar('*');
tTwo.add(birthday);
tTwo.add(year);
year.addActionListener(new birthday(year));
tTwo.add(y);
tTwo.add(month);
month.addActionListener(new birthday(month));
tTwo.add(m);
tTwo.add(day);
day.addActionListener(new birthday(day));
tTwo.add(d);
tSix.add(sex);
group.add(man);
man.addActionListener(new sex());
group.add(woman);
woman.addActionListener(new sex());
tSeven.add(man);
tSeven.add(woman);
tSeven.setBorder(BorderFactory.createLineBorder(Color.gray));
tSix.add(tSeven);
tFour.add(interest, BorderLayout.WEST);
tFive.add(read);
read.addActionListener(new interest(read));
tFive.add(sing);
sing.addActionListener(new interest(sing));
tFive.add(dance);
dance.addActionListener(new interest(dance));
tFive.setBorder(BorderFactory.createLineBorder(Color.gray));
tFour.add(tFive);
tNine.add(remakes);
rText.setBorder(BorderFactory.createLineBorder(Color.gray));
tNine.add(rText);
rText.setLineWrap(true);
tNine.add(button);
button.addActionListener(new button());
tz.add(tOne);
tThree.add(tTwo, BorderLayout.NORTH);
tThree.add(tFour);
tz.add(tThree);
tEight.add(tz);
tTen.add(tSix);
tTen.add(tNine);
tEight.add(tTen);
add(tEight);
pack();
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private Integer[] data(int day, int j) {
Integer[] data = new Integer[day];
data[0] = null;
if (j == 0)
j = 1;
for (int i = 1; i data.length; i++) {
data[i] = j + i - 1;
}
return data;
}
class Textaction implements ActionListener {
private JTextField text;
public Textaction(JTextField JTF) {
text = JTF;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == aText) {
name = text.getText();
FocusManager.getCurrentManager().focusNextComponent();
}
if (e.getSource() == pText) {
FocusManager.getCurrentManager().focusNextComponent();
pass1 = text.getText();
}
if (e.getSource() == cText) {
pass2 = text.getText();
FocusManager.getCurrentManager().focusNextComponent();
}
}
}
class birthday implements ActionListener {
private JComboBox combo = new JComboBox();
public birthday(JComboBox JCB) {
combo = JCB;
}
public void actionPerformed(ActionEvent e) {
birth += combo.getSelectedItem() + " ";
}
}
class sex implements ActionListener {
public void actionPerformed(ActionEvent e) {
se = e.getActionCommand();
}
}
class interest implements ActionListener {
private JCheckBox check;
public interest(JCheckBox JBC) {
check = JBC;
}
public void actionPerformed(ActionEvent e) {
if (check.isSelected()) {
intre += e.getActionCommand() + " ";
}
}
}
class button implements ActionListener {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "您确定注册吗?", "请确认",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.OK_OPTION) {
remake = remakes.getText();
try {
FileWriter out = new FileWriter("D:/Information.txt", true);
out.write(name + " " + pass1 + " " + se + " " + birth + " "
+ intre + " " + remake + "\r\n");
out.close();
} catch (IOException io) {
io.getStackTrace();
}
}
}
}
}
分享文章:跳舞java动态代码,java动态执行代码
文章出自:http://cdiso.cn/article/dsgdcig.html