Java Bean Machine程序无法正确输出

时间:2014-03-01 02:04:00

标签: java

我正在研究这个java项目来模拟一个bean机器或者称为plinko游戏等。基本上该程序应该做的是询问用户他们想要丢弃多少个球以及他们想要多少个插槽底部。然后它应该击中钉子并且有50%的机会向左或向右下跌。然后应该显示每个球的路径,例如:LRLLLRL等。它最后做的是显示球如何堆叠的直方图。我已经获得了正确显示的路径,但无法获得直方图的正确输出。我想我有一点忽略了导致它的东西,但我想我会来这里看看是否有人可以指出它。如果有人能提供帮助,我会非常感激。

预期产出:

Balls: 5  
Slots: 7  

LRLRLLR  
RRLRRRL  
RLRLLRR  
LRRLRLL  
LRLRLRR  

 0  
00 0 0

这个输出不正确,但它给出了应该发生什么的想法。它应该打印出球的路径,然后打印出当所有球落入并堆叠起来时游戏的样子。

import java.util.Scanner;

public class KedgeBean{
public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    System.out.print("Enter the number of balls you want drop: ");

    int ball_number = input.nextInt();

    while(ball_number < 0)
    {
        System.out.print("Please enter a positive number: ");

        ball_number = input.nextInt();
    }

    System.out.print("Enter the number of slots in the machine: ");

    int slot_number = input.nextInt();

    while(slot_number < 0)
    {
        System.out.print("Please enter a positive number: ");

        slot_number = input.nextInt();
    }

    int[] slots_array = new int[slot_number];


    for(int i = 0; i < ball_number; i++)
    {
        slots_array[ball_path(slot_number)]++;
    }

    display(slots_array);       
}

/** Produces the output for the ball and also stores the position of the ball */
public static int ball_path (int slot_number)
{
    int ball_position = (slot_number / 2);

    for(int i = 0; i < slot_number; i++)
    {

        if(Math.random() * 10 <= 5)
        {
            ball_position++;

            if(ball_position >= slot_number)
            {
                ball_position--;
            }

            System.out.print("R");
        }

        else
        {
                    ball_position--;

            if(ball_position < 0)
            {
                ball_position++;
            }

            System.out.print("L");
        }
    }

    System.out.println();

    return ball_position;
}

/** display */
public static void display(int[] slots_array)
{
    int slot_height = maximum(slots_array);

    for(int i = slot_height; i > 0; i--)
    {
        for(int j = 0; j < slots_array.length; j++)
        {
            if(slots_array[j] >= 1)
            {
                System.out.print("o");
            }

            else
            {
                System.out.print("-");
            }

        }
    System.out.println();
    }
}

/** Gets the maximum number in a slot */
public static int maximum(int[] slots_array)
{
    int max = slots_array[0];

    for(int i = 1; i < slots_array.length; i++)
    {
        if(slots_array[i] > max)
        {
            max = slots_array[i];
        }
    }

return max; 

}
}

1 个答案:

答案 0 :(得分:0)

package deneme;

import java.util.Scanner;

public class BeanMachine {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of balls to drop: ");
        int numberOfBalls = input.nextInt();

        System.out.print("Enter the number of slots in the bean machine: ");
        int numberOfSlots = input.nextInt();

        int[] slots = new int[numberOfSlots + 1];
        String rotation;
        for (int i = 0; i < numberOfBalls; i++) {
                int sum = 0;
                for (int j = 0; j < numberOfSlots; j++) {
                    int randomNumber = (int) (Math.random() * 2);
                    sum += randomNumber;
                    if (randomNumber == 0)
                        rotation = "L";
                    else
                        rotation = "R";
                    System.out.print(rotation);
                }
                slots[sum]++;
                System.out.println();
        }
        String[] ball = new String[numberOfSlots + 1];
        for (int i = numberOfBalls; i > 0; i--) {
            for (int j = 0; j <= numberOfSlots; j++) {
                if (i == slots[j]) {
                    ball[j] = "O";
                    slots[j]--;
                } else
                    ball[j] = " ";
                System.out.print(ball[j]);
            }
            System.out.println();
        }
    }

}