0

我在我的第二个函数类型方法中设置了一个计数器,以便每次滚动添加到 1000 个滚动的“2”,它将在 main 方法中打印次数。除了值总是返回为 0,当程序运行时,忽略空的 if/else 块,我还没有填充那些因为我试图先修复一些对我有帮助的东西。

我已经尝试研究其他方法来实现这一点,除了二进制搜索似乎是最合理的,除非在这种情况下,要么我使用不正确,要么程序中出现了另一个问题。

import java.util.Scanner;
import java.util.Arrays;

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

    Scanner in = new Scanner (System.in);

    int[] counts = new int[1000];
    int [] counts2 = new int[1000];

    int userInput;
    String input;
    int counter = 0;


    System.out.println("Hello and welcome to the program");
    System.out.println("In this program two six sided dices will be rolled and one eleven sided dice will be rolled (1000 times each)");

    System.out.println("The dices will be rolled to determine the odds of how many times the roll 2 comes up on both dices and die");
    System.out.println("(Press any key to be prompted) with user Instructions");
    input = in.nextLine();

    System.out.println("To determine the amount of times snake eyes is rolled with two six sided dices press '1'");
    System.out.println("For the amount of times two is rolled on a eleven sided die press '2'");
    System.out.println("For both press '3', or to exit this program press '4' ");
    userInput = in.nextInt();
    in.nextLine();

    if (userInput == 1)
    {

      System.out.println("is " + counter);
    }

    else if (userInput == 2)
    {
      // enter code...(Not finished)
    }

    else if (userInput == 3)
    {
      // enter code...(Not finished)
    }

    else 
    {
      // enter code... (Not finished)
    }
  }
  // pass array into method
  void myMethod (int[] counts, int[] counts2)
  {
    for (int i = 0; i < counts.length; i++)
    {

      counts [i] = (int)(Math.random ()*6+1)+ (int)(Math.random ()*6+1);
      counts2 [i] = (int)(Math.random ()*10+2);
    }
  } // pass array into method
  public static int Secendmethod (int [] counts, int counter)
  {
    Arrays.binarySearch(counts, 2);
    for(int i = 0; i <counts.length; i++) 

      if (counts[i] == 2) {
      counter++;

    }
    return counter ;
  }  
}
4

2 回答 2

0

好的。首先将您的 myMethod() 更改为...

public static int[] create(int n)
{
    int[] temp = new int[1000];
    if(n == 6)
    {            
        for (int i = 0; i < 1000; i++)
        {
            temp[i] = (int)(Math.random ()*6+1);
        }
    }
    if(n == 11)
    {
        for (int i = 0; i < 1000; i++)
        {
            temp[i] = (int)(Math.random ()*10+2);
        }
    }
    return temp;
}

还将 Secondmethod 更改为...

public static int Secendmethod(int[] ar)
{
    int counter = 0;
    for(int i = 0; i < 1000; i++) 
    {
        if (ar[i] == 2) 
        {
            counter++;
        }
    }
    return counter ;
}  

然后将您的 if 语句更改为:

if (userInput == 1)
{
  counts = create(6);
  System.out.println("is " + Secondmethod(counts));

}

对于 userInput 是两个,使第一行:counts2 = create(11); 第二个: System.out.println("is " + Secondmethod(counts2));

祝你好运,随时提问!

于 2013-03-25T22:03:51.127 回答
0

这是我将如何比较一卷 2

    import java.util.Scanner;
import java.util.Arrays;

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

    Scanner in = new Scanner (System.in);

    int[] counts = new int[1000];
    int[] counts2 = new int[1000];

    int userInput;
    String input;
    int counter = 0;

    userInput = in.nextInt();
    in.nextLine();

    if (userInput == 1){ //used in your previous code
        rollDice(counts,6); //populates array count with Random int by 6
        rollDice(counts2,6); //populates array count with Random int by 6
        counter = compareDoubles(counts,counts2,1);
        System.out.println("is " + counter);
    }

  }
  /*
   * count: Array to store resulting dice rolls
   * diceSize: number of different results from rolling dice
   */
  static void rollDice(int[] counts, int diceSize){
        for (int i = 0; i < counts.length; i++){
            counts [i] = (int)(Math.random ()*diceSize+1); 
        }
  }
  /*
   * firstCounts: first set of numbers
   * secondCounts: second set of numbers
   * num: number to compare both sets against
   * returns number of times both sets evaluate to num
   */
  public static int compareDoubles (int [] firstCounts, int[] secondCounts, int num)  {
    int amount = 0;

    if(firstCounts.length <= secondCounts.length){ //make sure we don't get arrayOutOfBounds
    for(int i = 0; i <firstCounts.length; i++) 
        if (firstCounts[i] == num && secondCounts[i] == num) 
            amount++;
    }
    return amount;
  }  
}

*编辑

如果以后不使用数组,(仅用于计算掷骰子,效率不高)。我扩展了掷骰子以返回所有骰子掷出一个数字的次数。

要以更有效和动态的方式执行我们上面所做的事情,请调用

counter = rollDice(1000, 2, 6, 1);

这将去

     /*
           * rolls: number of times a dice is thrown
           * dice amount: number of di being thrown
           * diceSize: number of different results from rolling dice
           * number: number of times all dice land on number
           */
          static int rollDice(int rolls, int diceAmount, int diceSize, int number){
                int amount = 0;

                for (int i = 0; i < rolls; i++){
                    int[] dice = new int[diceAmount];
                    boolean alltrue = true;
                    for (int j = 0; j < diceAmount; j++){ //Roll all the dice to a random number by diceSize
                        dice[j] = (int)(Math.random ()*diceSize+1); 
                    }

                    for (int j = 0; j < diceAmount; j++){ //Recount all the dice rolled to see if they all landed the same way
                        if(dice[j] != number)
                             alltrue=false;
                }
                if(alltrue) //All dice were the number
                    amount++;

            }
            return amount;
      }
于 2013-03-25T21:05:20.700 回答