0

2番目の関数型メソッドでカウンターを設定しました。これにより、ロールが1000ロールの「2」に追加されるたびに、メインメソッドでの回数が出力されます。値が常に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

これが私が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 に答える
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が2の場合、最初の行を作成します。counts2 = create(11); 2番目:System.out.println( "is" + Secondmethod(counts2));

頑張って、気軽に質問してください!

于 2013-03-25T22:03:51.127 に答える