Most of the time, there are tangled things, such as what to do and what not to do. It is said that tossing a coin is very effective, not because of the result of tossing a coin, but in the moment when the coin is thrown, it is obvious what the heart wants. But it's impossible to carry a coin with you. So there's this little piece of software.
Core algorithms
Ha, this is not the core algorithm. After all, not their own. is an algorithm that generates random numbers. Let's track it down a bit.
//第一步:入口之Math.random()Math.random();
//第二步:进入random的内部publicstaticdoublerandom() { return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); }
//第三步:深入下去publicdoublenextDouble() { return (((long)(next(2627) + next(27)) * DOUBLE_UNIT; }
//第四步:继续研究 privatestaticfinaldouble0x1.0p-53// 1.0 / (1L << 53)
In this way, we have traced the principle of the random algorithm that comes with Java. Yes, that's the left shift of the columns, so that we can get a more reasonable random number. Of course, the direct random number is a double type of data, and we cannot use it directly, to make the relevant conversions.
The idea of this little program
Say is the idea, in fact, every step has been completed something.
Here I use the AWT package to build the interface, and then use a list as the container for the options, plus some custom methods to implement.
Function Description:
The main interface can be used to get random results, and then click the Add button to go to add a custom menu effect. Added when the latest options will be pinned, enter or click the Add button to complete the Add. Click Finish to go back to the main screen to continue to help you choose.
My source
PackageToolsImportJava.awt.BorderLayout;ImportJava.awt.Button;ImportJava.awt.Color;ImportJava.awt.FlowLayout;ImportJava.awt.Font;ImportJava.awt.Frame;ImportJava.awt.Label;ImportJava.awt.Panel;ImportJava.awt.TextArea;ImportJava.awt.TextField;ImportJava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;ImportJava.awt.event.WindowEvent;ImportJava.util.ArrayList;ImportJava.util.List;ImportJavax.swing.JOptionPane;/** * Date: May 6, 2016 9:36:53 * Function Description: A simple Java AWT applet, you can manually add optional, randomly help to make a choice * * algorithm: Using the Java self-contained random algorithm for the list of elements of the random selection * * @author Administrator * * * * Public class choosehelper { /** * Realization of the idea: first, a display of the main interface for the display of results and user interaction, when the click on the Add button will automatically pop up an add interface * in the Add interface, users can either browse to the original options, you can also see the newly added options in real time (here The item is pinned, easy to observe) * After clicking the "Finish" button, you can go back to the original interface and get the result of the new random event. * Note: There is no layered design because the program is too small, layered words appear too cumbersome. * There is no detailed procedure for setting up layouts and various listening events, so you should minimize the existence of redundant code. */ Public Choosehelper() {Frame frame =NewFrame (); Frame.settitle ("Help me make my choice."); list<string> list =NewArraylist<string> (); List.add ("One More time!" "); Label label =NewLabel ("", Label.center); Label.setfont (NewFont ("Arial Bold", Font.Italic, +)); Label.setforeground (Color.decode ("#00FFFF")); Frame.setlayout (NewBorderLayout ()); Frame.add (label, Borderlayout.center); Panel panel =NewPanel (NewFlowLayout ()); Button AddItem =NewButton ("Add Item"); Button Doitem =NewButton ("Random"); Panel.add (AddItem); Panel.add (Doitem); Frame.add (panel, Borderlayout.south); Frame.setvisible (true); Frame.setsize ( -, -); Frame.setlocationrelativeto (NULL); Frame.addwindowlistener (NewWindowadapter () {@Override Public void windowclosing(WindowEvent e) {System.exit (0); } });/** * Click this button to pop up an interface to add an alternative */Additem.addactionlistener (NewActionListener () {@Override Public void actionperformed(ActionEvent e) {Frame Addframe =NewFrame (); Addframe.settitle ("Add an alternate"); Addframe.setvisible (true); Addframe.setsize ( -, -); Addframe.setlocationrelativeto (NULL); Addframe.addwindowlistener (NewWindowadapter () { Public void windowclosing(WindowEvent e) {addframe.setvisible (false); Addframe.dispose (); }; }); TextArea TA =NewTextArea (); Ta.seteditable (false); Ta.setfocusable (false); Refreshlist (TA, list); Addframe.add (TA, borderlayout.center); Panel bottom =NewPanel (NewFlowLayout ()); TextField tf =NewTextField ( -); Button ADDTF =NewButton ("Add this content"); Button Complete =NewButton ("Done"); Bottom.add (TF); Bottom.add (ADDTF); Bottom.add (complete); Tf.addactionlistener (NewActionListener () { Public void actionperformed(ActionEvent e) {String item = Tf.gettext (). toString ();if(Item! =NULL&&!item.equals ("") {List.add (0, item); Tf.settext (""); Refreshlist (TA, list); }Else{Joptionpane.showmessagedialog (NULL,"Please enter the activity you want to make!" "); } } }); Addframe.add (bottom, Borderlayout.south); Complete.addactionlistener (NewActionListener () { Public void actionperformed(ActionEvent e) {addframe.setvisible (false); Addframe.dispose (); } }); Addtf.addactionlistener (NewActionListener () { Public void actionperformed(ActionEvent e) {String item = Tf.gettext (). toString ();if(Item! =NULL&&!item.equals ("") {List.add (0, item); Tf.settext (""); Refreshlist (TA, list); }Else{Joptionpane.showmessagedialog (NULL,"Please enter the activity you want to make!" "); } } }); }/** * The task of refreshing the list is extracted, which makes it easy to refresh the display of the results each time. * * @param ta * for displaying refreshed results * @param list * Refreshes the contents of the list into the container textarea above * / Private void refreshlist(TextArea ta, list<string> List) {Ta.settext (""); for(inti =0; I < list.size (); i++) {ta.append (List.get (i) +"\ n"); } } }); Doitem.addactionlistener (NewActionListener () {@Override Public void actionperformed(ActionEvent e) {intLength = List.size (); Label.settext (List.get (int) (Math.random () * length))); } }); }/** * Main program entrance, test the operation results of the program * * @param args */ Public Static void Main(string[] args) {Choosehelper helper =NewChoosehelper (); }}
Back to Summary
The logic here is still relatively clear, due to the small amount of code. So there is no layering, and a lot of repetitive code is not extracted into a method, this is not good place, I hope you do not do so, as far as possible with the object-oriented thinking of modular programming.
Then there is the prospect. In fact, there are many places in this program that can be perfected, for example, when adding menu items, we may not want to add them one at a time. We can use a file selector to read from a local file. XML file or a text file or a database ah what is on the network is OK, so it is necessary to write a relatively low level of the DAO layer processing. It will not be difficult to achieve.
Program Run results show
Professional treatment Choice Difficult----help you choose