JAVA Game Bean Machine__java

Source: Internet
Author: User
Project Requirements

Input the number of balls and the number of slots, beans in the nail according to the first few lines have several nails arranged, the number of rows (Groove-1).

The rolling direction of each ball is printed out and the distribution of the balls in each slot is shown.

The code is as follows

Import Java.util.Arrays;

Import Java.util.Scanner;

	public class Beanmachine {static Scanner input = new Scanner (system.in);
		public static void Main (string[] args) {System.out.println ("Enter the number of balls to drop:");
		int beannums = Input.nextint ();
		System.out.println ("Enter the number of slots in the Bean Machine:");
		int slots = Input.nextint ();
	Showanswer (beannums, slots);
		public static void Showanswer (int beannums, int slots) {int direction;
		int[] Dir = new Int[beannums];
		
		Int[][] slo = new Int[slots][beannums];

				A ball roll down for (int i = 0; i < beannums ++i) {//Nail line number is less than 1 for (int j = 0; j < slots-1; ++j) {
				
				Direction indicates that the ball is left or right, taking a random number of 0 or 1 direction = (int) (Math.random () * 2); When the direction is 0 o'clock, remember to turn left;
				Dir[i] for the first small ball rolling process several times to the right roll.

				if (direction = = 0) {System.out.print ("L");
					else {System.out.print ("R");
				dir[i]++;
		} System.out.println (); for (int j = 0; J < Slots -1;
			
			++J) {//slo[j][count] indicates how many balls int count = BeanNums-1 are the number of J slots from bottom to top;
					for (int i = 0; i < beannums ++i) {if (dir[i] = = j) {Slo[j][count] = 1;
				count--;  }}///show slots for (int i = 0; i < beannums ++i) {//nail row count less than 1 for (int j = 0; j < slots-1;
				++J) {if (slo[j][i] = = 1) {System.out.print ("0");
				else {System.out.print ("");
		} System.out.println ();
 }
	}
}

Run Results
Enter the number of balls to drop: 5 Enter the number of slots in the
Bean machine:
8
Lrrrllr
lllrllr< C6/>LLRRLRR
rrlrlll
lllrrrr
       
       
    0  
    0  
  000  


The topic comes from the Java language Programming program (p230-6.21***)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.