成都创新互联网站制作重庆分公司

java实现代码跳舞 java跳一跳程序代码

数据结构中的舞伴问题。用JAVA实现

/**

10年专注成都网站制作,成都企业网站定制,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于成都企业网站定制,高端网页制作,对成都木屋等多个方面,拥有多年的网站运维经验。

假设在周末舞会上,男士们(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数据结构跳舞配对问题(队列的应用)

代码如下,可以直接运行。

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编程 设计一个图形用户界面。界面包括三个单选按钮、两个复选框、一个列表、一个文本区和一个按

程序如下:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

public class JFrameDemo extends JFrame implements ActionListener

{

private JPanel panel;

private JButton button;

private JTextArea textArea;

private JCheckBox musicBox;

private JCheckBox danceBox;

private JRadioButton hanButton;

private JRadioButton manButton;

private JRadioButton huiButton;

private ButtonGroup buttonGroup;

public JFrameDemo()

{

panel = new JPanel();

button = new JButton("确定");

textArea = new JTextArea(40,30);

musicBox = new JCheckBox("唱歌");

danceBox = new JCheckBox("跳舞");

huiButton = new JRadioButton("回族");

hanButton = new JRadioButton("汉族");

manButton = new JRadioButton("满族");

buttonGroup = new ButtonGroup();

buttonGroup.add(huiButton);

buttonGroup.add(hanButton);

buttonGroup.add(manButton);

panel.setLayout(new FlowLayout(3));

panel.add(huiButton);

panel.add(hanButton);

panel.add(manButton);

panel.add(musicBox);

panel.add(danceBox);

panel.add(button);

panel.add(textArea);

add(panel);

setTitle("选择兴趣爱好");

setBounds(100, 100, 400, 280);

setResizable(false);

setVisible(true);

this.button.addActionListener(this);

}

public static void main(String[] args)

{

new JFrameDemo();

}

@Override

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == this.button)

{

String info = "";

if(this.huiButton.isSelected())

{

info += this.huiButton.getText() + "\n";

}

if(this.hanButton.isSelected())

{

info += this.hanButton.getText() + "\n";

}

if(this.manButton.isSelected())

{

info += this.manButton.getText() + "\n";

}

if(this.danceBox.isSelected())

{

info += this.danceBox.getText() + "\n";

}

if(this.musicBox.isSelected())

{

info += this.musicBox.getText() + "\n";

}

this.textArea.setText(info);

}

}

}

有问题欢迎提问,满意请采纳,谢谢!


本文名称:java实现代码跳舞 java跳一跳程序代码
当前路径:http://cxhlcq.com/article/dogihcp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部