java實現(xiàn)代碼跳舞 java跳一跳程序代碼

數(shù)據(jù)結(jié)構(gòu)中的舞伴問題。用JAVA實現(xiàn)

/**

10年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站定制,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對成都木屋等多個方面,擁有多年的網(wǎng)站運維經(jīng)驗。

假設(shè)在周末舞會上,男士們(m人)和女士們(n人)進入舞廳時,各自排成一隊。跳舞開始時,依次從男隊和女隊的隊頭上各出一人配成舞伴。若兩隊初始人數(shù)不相同,則較長的那一隊中未配對者等待下一輪舞曲。現(xià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("請輸入男士人數(shù):");

m=input();

System.out.println("請輸入女士人數(shù):");

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("輸入數(shù)據(jù)有誤!請重新輸入,退出請按exit");

line=br.readLine();

}

}

return returnNum;

}

}

Java數(shù)據(jù)結(jié)構(gòu)跳舞配對問題(隊列的應(yīng)用)

代碼如下,可以直接運行。

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 = "";// 記錄結(jié)果,即第二個問題

// 初始化,假設(shè)隊列存放男女生編號,從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) {//為了不死循環(huán),這里假設(shè)最多有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++;

}

// 結(jié)果

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語言設(shè)計程序?qū)崿F(xiàn):假設(shè)舞會上男女生各自一排,每次排頭牽手跳舞

6 5 1 2 結(jié)果才是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("請輸入男排人數(shù):");

int boyArray = sc.nextInt();

System.out.println("請輸入女排人數(shù):");

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編程 設(shè)計一個圖形用戶界面。界面包括三個單選按鈕、兩個復(fù)選框、一個列表、一個文本區(qū)和一個按

程序如下:

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

}

}

}

有問題歡迎提問,滿意請采納,謝謝!

當(dāng)前題目:java實現(xiàn)代碼跳舞 java跳一跳程序代碼
網(wǎng)頁路徑:http://www.muchs.cn/article28/dogihcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司、虛擬主機、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)