簡體   English   中英

如何從隨機數中排序值?

[英]How to sort the value from random numbers?

for(x1 = 0; x1 < 26; x1++)
{
    index = (int)Math.round(Math.random() * ((arrnamex.size())-1) + 0);
    x=arrnamex.get(index);
    //x is for choosing a case
    y=arrnamey.get(index);
    //same with y
    arrnamex.remove(index);

    arrnamey.remove(index);
    posX.add(x);
    posY.add(y);
}

我唯一的問題是如何從隨機數升序排序

將您的隨機數添加到列表中。

排序列表。

 SortedSet<Integer> set = new TreeSet<Integer>(); 

現在添加要set隨機數。

我的程序太長了..但這是我真正需要的,所以我希望你能理解..(這不是整個程序,但我希望你仍然可以幫助我)

import java.awt.*;
import java.awt.Color.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Board extends JComponent      implements ActionListener
{

    JPanel playerPanel = new JPanel()
    {
        public void paintComponent(Graphics g)
        {  
        super.paintComponent(g);

            Image img =                 Toolkit.getDefaultToolkit().getImage(Board.class.getResource("BackgroundBOARD.jpg"));  
            g.drawImage(img,0,0,this.getWidth(),this.getHeight(), this); 
        }};
    JFrame frame = new JFrame("BOARD");
public static JLabel name = new JLabel();
public static int mypiece = 4;
public static ArrayList<Integer> arrnamex= new    ArrayList<Integer>(Arrays.asList(274,367,367,459,459,570,367,665,459,459,570,570,665,665,765,765,871,871,665,963,765,871,871,963,963,1067));//25 elems of x axis
public static ArrayList<Integer> arrnamey= new ArrayList<Integer>(Arrays.asList(325,325,225,282,172,225,445,172,392,502,325,445,282,392,225,325,172,282,502,225,445,502,392,445,325,325));//25 elems of y axis
public static ArrayList<Integer> posX = new ArrayList<Integer>();
public static ArrayList<Integer> posY = new ArrayList<Integer>();
public static ArrayList<JButton> allPiece = new ArrayList<JButton>();
public static ArrayList<JButton> userPiece = new ArrayList<JButton>();
public static ArrayList<JButton> NorthPiece = new ArrayList<JButton>();
public static ArrayList<JButton> WestPiece = new ArrayList<JButton>();
public static ArrayList<JButton> EastPiece = new ArrayList<JButton>();
public static int x = 0;
public static int y = 0;
public static int index = 0;
public static int indey = 0;
public static int x1=0;

 JButton wdash = new JButton("");
 JButton w1 = new JButton("");
 JButton w2 = new JButton("");
 JButton w3 = new JButton("");
 JButton w4 = new JButton("");
 JButton w5 = new JButton("");
 JButton w6 = new JButton("");
 JButton w7 = new JButton("");
 JButton w8 = new JButton("");
 JButton w9= new JButton("");
 JButton w10 = new JButton("");
 JButton w11 = new JButton("");
 JButton wzero = new JButton("");

 JButton bdash = new JButton();
 JButton b1 = new JButton("");
 JButton b2 = new JButton("");
 JButton b3 = new JButton("");
 JButton b4 = new JButton("");
 JButton b5 = new JButton("");
 JButton b6 = new JButton("");
 JButton b7 = new JButton("");
 JButton b8 = new JButton("");
 JButton b9= new JButton("");
 JButton b10 = new JButton("");
 JButton b11 = new JButton("");
 JButton bzero = new JButton("");




 public void launch()
 {
allPiece.add(wdash);
allPiece.add(w1);
allPiece.add(w2);
allPiece.add(w3);
allPiece.add(w4);
allPiece.add(w5);
allPiece.add(w6);
allPiece.add(w7);
allPiece.add(w8);
allPiece.add(w9);
allPiece.add(w10);
allPiece.add(w11);
allPiece.add(wzero);
allPiece.add(bdash);
allPiece.add(b1);
allPiece.add(b2);
allPiece.add(b3);
allPiece.add(b4);
allPiece.add(b5);
allPiece.add(b6);
allPiece.add(b7);
allPiece.add(b8);
allPiece.add(b9);
allPiece.add(b10);
allPiece.add(b11);
allPiece.add(bzero);
playerPanel.add(wdash);wdash.addActionListener(this);
playerPanel.add(w1);w1.addActionListener(this);
playerPanel.add(w2);w2.addActionListener(this);
playerPanel.add(w3);w3.addActionListener(this);
playerPanel.add(w4);w4.addActionListener(this);
playerPanel.add(w5);w5.addActionListener(this);
playerPanel.add(w6);w6.addActionListener(this);
playerPanel.add(w7);w7.addActionListener(this);
playerPanel.add(w8);w8.addActionListener(this);
playerPanel.add(w9);w9.addActionListener(this);
playerPanel.add(w10);w10.addActionListener(this);
playerPanel.add(w11);w11.addActionListener(this);
playerPanel.add(wzero);wzero.addActionListener(this);

playerPanel.add(bdash);bdash.addActionListener(this);
playerPanel.add(b1);b1.addActionListener(this);
playerPanel.add(b2);b2.addActionListener(this);
playerPanel.add(b3);b3.addActionListener(this);
playerPanel.add(b4);b4.addActionListener(this);
playerPanel.add(b5);b5.addActionListener(this);
playerPanel.add(b6);b6.addActionListener(this);
playerPanel.add(b7);b7.addActionListener(this);
playerPanel.add(b8);b8.addActionListener(this);
playerPanel.add(b9);b9.addActionListener(this);
playerPanel.add(b10);b10.addActionListener(this);
playerPanel.add(b11);b11.addActionListener(this);
playerPanel.add(bzero);bzero.addActionListener(this);
for(x1=0;x1<26;x1++)
{
    index = (int)Math.round(Math.random() * ((arrnamex.size())-1) + 0);
    x=arrnamex.get(index);
    y=arrnamey.get(index);
    arrnamex.remove(index);
    arrnamey.remove(index);
    posX.add(x);
    posY.add(y);

}
wdash.setBounds(posX.get(0),posY.get(0),60,70);
bdash.setBounds(posX.get(1),posY.get(1),60,70);
w1.setBounds(posX.get(2),posY.get(2),60,70);
b1.setBounds(posX.get(3),posY.get(3),60,70);
w2.setBounds(posX.get(4),posY.get(4),60,70);
b2.setBounds(posX.get(5),posY.get(5),60,70);
w3.setBounds(posX.get(6),posY.get(6),60,70);
b3.setBounds(posX.get(7),posY.get(7),60,70);
w4.setBounds(posX.get(8),posY.get(8), 60,70);
b4.setBounds(posX.get(9),posY.get(9),60,70);
w5.setBounds(posX.get(10),posY.get(10),60,70);
b5.setBounds(posX.get(11),posY.get(11),60,70);
w6.setBounds(posX.get(12),posY.get(12),60,70);
b6.setBounds(posX.get(13),posY.get(13),60,70);
w7.setBounds(posX.get(14),posY.get(14),60,70);
b7.setBounds(posX.get(15),posY.get(15),60,70);
w8.setBounds(posX.get(16),posY.get(16),60,70);
b8.setBounds(posX.get(17),posY.get(17),60,70);
w9.setBounds(posX.get(18),posY.get(18),60,70);
b9.setBounds(posX.get(19),posY.get(19),60,70);
w10.setBounds(posX.get(20),posY.get(20),60,70);
b10.setBounds(posX.get(21),posY.get(21),60,70);
w11.setBounds(posX.get(22),posY.get(22),60,70);
     b11.setBounds(posX.get(23),posY.get(23),60,70);
wzero.setBounds(posX.get(24),posY.get(24),60,70);
bzero.setBounds(posX.get(25),posY.get(25),60,70);

wdash.setForeground(Color.black);
wdash.setBackground(Color.white);
w1.setForeground(Color.black);
w1.setBackground(Color.white);
w2.setForeground(Color.black);
w2.setBackground(Color.white);
w3.setForeground(Color.black);
w3.setBackground(Color.white);
w4.setForeground(Color.black);
w4.setBackground(Color.white);
w5.setForeground(Color.black);
w5.setBackground(Color.white);
w6.setForeground(Color.black);
w6.setBackground(Color.white);
w7.setForeground(Color.black);
w7.setBackground(Color.white);
w8.setForeground(Color.black);
w8.setBackground(Color.white);
w9.setForeground(Color.black);
w9.setBackground(Color.white);
w10.setForeground(Color.black);
w10.setBackground(Color.white);
w11.setBackground(Color.white);
w11.setBackground(Color.white);
wzero.setForeground(Color.black);
wzero.setBackground(Color.white);

bdash.setForeground(Color.white);
bdash.setBackground(Color.black);
b1.setForeground(Color.white);
b1.setBackground(Color.black);
b2.setForeground(Color.white);
b2.setBackground(Color.black);
b3.setForeground(Color.white);
b3.setBackground(Color.black);
b4.setForeground(Color.white);
b4.setBackground(Color.black);
b5.setForeground(Color.white);
b5.setBackground(Color.black);
b6.setForeground(Color.white);
b6.setBackground(Color.black);
b7.setForeground(Color.white);
b7.setBackground(Color.black);
b8.setForeground(Color.white);
b8.setBackground(Color.black);
b9.setForeground(Color.white);
b9.setBackground(Color.black);
b10.setForeground(Color.white);
b10.setBackground(Color.black);
b11.setForeground(Color.white);
b11.setBackground(Color.black);
bzero.setForeground(Color.white);
bzero.setBackground(Color.black);





playerPanel.setLayout(null);
playerPanel.setPreferredSize(new  Dimension(1500,800));
playerPanel.setBounds(0,0,1500,800);
playerPanel.setOpaque(false);



frame.add(playerPanel);
frame.pack();
frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }//end ng launch
public static void player2()
{
    index= (int)Math.round(Math.random() * ((allPiece.size())-1) + 0);
    JButton btn1 = allPiece.get(index);

    if(mypiece==4)
    {
        btn1.setBounds(20,230,70,60);
        btn1.setEnabled(false);
    }
    else if (mypiece==3)
    {
        btn1.setBounds(20,300,70,60);
        btn1.setEnabled(false);
    }
    else if (mypiece==2)
    {
        btn1.setBounds(20,370,70,60);
        btn1.setEnabled(false);
    }
    else
    {
        btn1.setBounds(20,440,70,60);
        btn1.setEnabled(false);
    }
    allPiece.remove(index);
    WestPiece.add(btn1);
}
public static void player3()
 {
    index= (int)Math.round(Math.random() * ((allPiece.size())-1) + 0);
    JButton btn1 = allPiece.get(index);

    if(mypiece==4)
    {
        btn1.setBounds(1250,230,70,60);
        btn1.setEnabled(false);
    }
    else if (mypiece==3)
    {
        btn1.setBounds(1250,300,70,60);
        btn1.setEnabled(false);
    }
    else if (mypiece==2)
    {
        btn1.setBounds(1250,370,70,60);
        btn1.setEnabled(false);
    }
    else
    {
        btn1.setBounds(1250,440,70,60);
        btn1.setEnabled(false);
    }
    allPiece.remove(index);
    EastPiece.add(btn1);
}
   public void actionPerformed(ActionEvent e)
  {
   Object x = e.getSource();
if(x==wdash)
    {   
        if(mypiece==0)
        {
                  JOptionPane.showMessageDialog(null,"Panel Full");
        }

    else
    {
        if(mypiece==4)
        {
            allPiece.remove(wdash);
            System.out.println("Removed");
            userPiece.add(wdash);       
            System.out.println("Added");
            wdash.setBounds(500,620,60,70) ;
            wdash.setText("-");
            wdash.setBackground(Color.white);
            wdash.setForeground(Color.black);
            wdash.setEnabled(false);
            Board.player2();
            mypiece--;
        }
        else if(mypiece==3)
        {
            allPiece.remove(wdash);
            System.out.println("Removed");
            userPiece.add(wdash);       
            System.out.println("Added");
            wdash.setBounds(570,620,60,70) ;
            wdash.setText("-");
            wdash.setBackground(Color.white);
            wdash.setForeground(Color.black);
            wdash.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==2)
        {
            allPiece.remove(wdash);
            System.out.println("Removed");
            userPiece.add(wdash);       
            System.out.println("Added");
            wdash.setBounds(640,620,60,70) ;
            wdash.setText("-");
            wdash.setBackground(Color.white);
            wdash.setForeground(Color.black);
            wdash.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else
        {   
            allPiece.remove(wdash);
            System.out.println("Removed");
            userPiece.add(wdash);       
            System.out.println("Added");
            wdash.setBounds(710,620,60,70) ;
            wdash.setText("-");
            wdash.setBackground(Color.white);
            wdash.setForeground(Color.black);
            wdash.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }

    }
}
if(x==w1)
    {   
    if(mypiece==0)
    {
         JOptionPane.showMessageDialog(null,"Panel Full");
    }

    else
    {
        if(mypiece==4)
        {   
            allPiece.remove(w1);
            System.out.println("Removed");
            userPiece.add(w1);      
            System.out.println("Added");
            w1.setBounds(500,620,60,70) ;
            w1.setText("1");
            w1.setBackground(Color.white);
            w1.setForeground(Color.black);
            w1.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==3)
        {
            allPiece.remove(w1);
            System.out.println("Removed");
            userPiece.add(w1);      
            System.out.println("Added");
            w1.setBounds(570,620,60,70) ;
            w1.setText("1");
            w1.setBackground(Color.white);
            w1.setForeground(Color.black);
            w1.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==2)
        {
            allPiece.remove(w1);
            System.out.println("Removed");
            userPiece.add(w1);      
            System.out.println("Added");
            w1.setBounds(640,620,60,70) ;
            w1.setText("1");
            w1.setBackground(Color.white);
            w1.setForeground(Color.black);
            w1.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else
        {
            allPiece.remove(w1);
            System.out.println("Removed");
            userPiece.add(w1);      
            System.out.println("Added");
            w1.setBounds(710,620,60,70) ;
            w1.setText("1");
            w1.setBackground(Color.white);
            w1.setForeground(Color.black);
            w1.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
    }

    }
if(x==w2)
    {   
    if(mypiece==0)
        {
            JOptionPane.showMessageDialog(null,"Panel Full");
        }

    else
    {
        if(mypiece==4)
        {
            allPiece.remove(w2);
            System.out.println("Removed");
            userPiece.add(w2);  
            System.out.println("Added");    
            w2.setBounds(500,620,60,70) ;
            w2.setText("2");
            w2.setBackground(Color.white);
            w2.setForeground(Color.black);
            w2.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;

        }
        else if(mypiece==3)
        {
            allPiece.remove(w2);
            System.out.println("Removed");
            userPiece.add(w2);  
            System.out.println("Added");    
            w2.setBounds(570,620,60,70) ;
            w2.setText("2");
            w2.setBackground(Color.white);
            w2.setForeground(Color.black);
            w2.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==2)
        {
            allPiece.remove(w2);
            System.out.println("Removed");
            userPiece.add(w2);  
            System.out.println("Added");    
            w2.setBounds(640,620,60,70) ;
            w2.setText("2");
            w2.setBackground(Color.white);
            w2.setForeground(Color.black);
            w2.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==1)
        {
            allPiece.remove(w2);
            System.out.println("Removed");
            userPiece.add(w2);  
            System.out.println("Added");    
            w2.setBounds(710,620,60,70) ;
            w2.setText("2");
            w2.setBackground(Color.white);
            w2.setForeground(Color.black);
            w2.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
    }
}   
if(x==w3)
    {   
    if(mypiece==0)
        {
            JOptionPane.showMessageDialog(null,"Panel Full");
        }

    else
    {
        if(mypiece==4)
        {
            allPiece.remove(w3);
            System.out.println("Removed");
            userPiece.add(w3);      
            System.out.println("Added");
            w3.setBounds(500,620,60,70) ;
            w3.setText("3");
            w3.setBackground(Color.white);
            w3.setForeground(Color.black);
            w3.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==3)
        {
            allPiece.remove(w3);
            System.out.println("Removed");
            userPiece.add(w3);      
            System.out.println("Added");
            w3.setBounds(570,620,60,70) ;
            w3.setText("3");
            w3.setBackground(Color.white);
            w3.setForeground(Color.black);
            w3.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else if(mypiece==2)
        {
            allPiece.remove(w3);
            System.out.println("Removed");
            userPiece.add(w3);      
            System.out.println("Added");
            w3.setBounds(640,620,60,70) ;
            w3.setText("3");
            w3.setBackground(Color.white);
            w3.setForeground(Color.black);
            w3.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }
        else 
        {
            allPiece.remove(w3);
            System.out.println("Removed");
            userPiece.add(w3);      
            System.out.println("Added");
            w3.setBounds(710,620,60,70) ;
            w3.setText("3");
            w3.setBackground(Color.white);
            w3.setForeground(Color.black);
            w3.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }   
    }

}   
if(x==w4)
    {   
    if(mypiece==0)
        {
            JOptionPane.showMessageDialog(null,"Panel Full");
        }

    else
    {
        if(mypiece==4)
        {
            allPiece.remove(w4);
            System.out.println("Removed");
            userPiece.add(w4);      
            System.out.println("Added");
            w4.setBounds(500,620,60,70) ;
            w4.setText("4");
            w4.setBackground(Color.white);
            w4.setForeground(Color.black);
            w4.setEnabled(false);
            Board.player2();
            Board.player3();
            mypiece--;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM