无法获得多个随机数以匹配我的五个猜测

时间:2015-06-27 15:14:50

标签: java

有人可以向我解释为什么我似乎无法获得多个随机数来匹配我的猜测?这里的目标是生成一个随机数,然后要求用户输入五个数字,看看有多少匹配。我使用临时JOptionPane(不在下面的代码中)来显示随机数是什么,但它只显示一个随机值。问题可能在数组中,还是因为我生成了一个随机数?

public class Test1 {

   public static void main(String[] args) {

      final int MIN_NUM = 1;
      final int MAX_NUM = 5;
      int count = 0;

      int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
      int guess = getUserGuess();
      boolean determineMatch = isGuessCorrect(randomNum, guess);        
      if (determineMatch == true) {
         count++;
      }        
      displayResult(count);

   }

   public static int getRandomNum(int MIN_NUM, int MAX_NUM) {

      return (int)(Math.random() * MAX_NUM) + MIN_NUM;

   }

   public static int getUserGuess() {

      final int MIN_GUESS = 1;
      final int MAX_GUESS = 5;
      int x = 0;

      int[] guess = new int[MAX_GUESS];

      for (int i = 0; i < guess.length; i++) {

         do {

            try {
               guess[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
            }

            catch (NumberFormatException e) {
               guess[i] = 0;
            }

            if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
               JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
            }

         } while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);

      }

      return guess[x];

   }

   public static boolean isGuessCorrect(int randomNum, int guess) {

      boolean determineMatch = true;

      if (randomNum == guess) {
         determineMatch = true;
      }
      else {
         determineMatch = false;
      }

      return determineMatch;

   }

   public static void displayResult(int count) {

      if (count >= 0 || count <= 1) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou don’t have any supernatural powers. Sorry!");
      }
      else if (count >= 2 || count <= 3) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou might be good. Try again another time.");
      }
      else if (count >= 4 || count <= 5) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou’re hired! When can you start?");
      }

   }

}

3 个答案:

答案 0 :(得分:2)

您的课程有多个问题,首先您只生成一个随机数,第二个您只匹配第一个答案。以下是修订版

package com.test;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class Test1 {

    public static void main(String[] args) {

        final int MIN_NUM = 1;
        final int MAX_NUM = 5;
        int count = 0;

        int[] guesses = getUserGuess();

        List<Integer> randomNums = getRandomNum(MIN_NUM, MAX_NUM);

        for (int i = 0; i < guesses.length; i++) {
            boolean determineMatch = isGuessCorrect(randomNums, guesses[i]);
            if (determineMatch == true) {
                count++;
            }
        }
        displayResult(count);

    }

    public static List<Integer> getRandomNum(int MIN_NUM, int MAX_NUM) {
        List<Integer> randomNums = new ArrayList<Integer>();
        for (int i = MIN_NUM; i <= MAX_NUM; i++) {
            randomNums.add((int) (Math.random() * MAX_NUM) + MIN_NUM);
        }
        return randomNums;
    }

    public static int[] getUserGuess() {

        final int MIN_GUESS = 1;
        final int MAX_GUESS = 5;

        int[] guess = new int[MAX_GUESS];

        for (int i = 0; i < guess.length; i++) {
            do {
                try {
                    guess[i] = Integer
                            .parseInt(JOptionPane
                                    .showInputDialog("Enter a number between 1 and 5:"));
                }

                catch (NumberFormatException e) {
                    guess[i] = 0;
                }

                if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
                    JOptionPane.showMessageDialog(null,
                            "ERROR! Enter a number between 1 and 5");
                }

            } while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);

        }

        return guess;

    }

    public static boolean isGuessCorrect(List<Integer> randomNums, int guess) {

        boolean determineMatch = true;

        if (randomNums.contains(guess)) {
            determineMatch = true;
        } else {
            determineMatch = false;
        }

        return determineMatch;

    }

    public static void displayResult(int count) {

        if (count >= 0 || count <= 1) {
            JOptionPane
                    .showMessageDialog(
                            null,
                            "Out of 5 tries, you guessed "
                                    + count
                                    + " correct.\nYou don’t have any supernatural powers. Sorry!");
        } else if (count >= 2 || count <= 3) {
            JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
                    + count
                    + " correct.\nYou might be good. Try again another time.");
        } else if (count >= 4 || count <= 5) {
            JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
                    + count + " correct.\nYou’re hired! When can you start?");
        }

    }

}

以下是修订版:

package com.test;

import javax.swing.JOptionPane;

public class Test1 {

    public static void main(String[] args) {

        final int MIN_NUM = 1;
        final int MAX_NUM = 5;
        int count = 0;

        int[] guesses = getUserGuess();

        int randomNum = getRandomNum(MIN_NUM, MAX_NUM);

        for (int i = 0; i < guesses.length; i++) {
            boolean determineMatch = isGuessCorrect(randomNum, guesses[i]);
            if (determineMatch == true) {
                count++;
            }
        }
        displayResult(count);

    }

    public static int getRandomNum(int MIN_NUM, int MAX_NUM) {
        return (int) (Math.random() * MAX_NUM) + MIN_NUM;
    }

    public static int[] getUserGuess() {

        final int MIN_GUESS = 1;
        final int MAX_GUESS = 5;

        int[] guess = new int[MAX_GUESS];

        for (int i = 0; i < guess.length; i++) {
            do {
                try {
                    guess[i] = Integer
                            .parseInt(JOptionPane
                                    .showInputDialog("Enter a number between 1 and 5:"));
                }

                catch (NumberFormatException e) {
                    guess[i] = 0;
                }

                if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
                    JOptionPane.showMessageDialog(null,
                            "ERROR! Enter a number between 1 and 5");
                }

            } while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);

        }

        return guess;

    }

    public static boolean isGuessCorrect(int randomNum, int guess) {

        boolean determineMatch = true;

        if (randomNum == guess) {
            determineMatch = true;
        } else {
            determineMatch = false;
        }

        return determineMatch;

    }

    public static void displayResult(int count) {

        if (count >= 0 || count <= 1) {
            JOptionPane
                    .showMessageDialog(
                            null,
                            "Out of 5 tries, you guessed "
                                    + count
                                    + " correct.\nYou don’t have any supernatural powers. Sorry!");
        } else if (count >= 2 || count <= 3) {
            JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
                    + count
                    + " correct.\nYou might be good. Try again another time.");
        } else if (count >= 4 || count <= 5) {
            JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
                    + count + " correct.\nYou’re hired! When can you start?");
        }

    }

}

答案 1 :(得分:1)

此功能有一些错误。

   public static int getUserGuess() {

      final int MIN_GUESS = 1;
      final int MAX_GUESS = 5;
      int x = 0; // <-- Problem with this variable. Trace the value of 'x' through this function - you are always returning guess[x] and x is always 0

      int[] guess = new int[MAX_GUESS];

      for (int i = 0; i < guess.length; i++) {

         do {

            try {
               guess[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
            }

            catch (NumberFormatException e) {
               guess[i] = 0;
            }

            if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
               JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
            }

         } while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);

      }

      return guess[x]; // This function returns a SINGLE integer rather than the entire array of inputs, so the call to isGuessCorrect() only checks one guess.

   }

考虑将getUserGuess的返回类型更改为整数数组:

   public static int[] getUserGuesses() {

      final int MIN_GUESS = 1;
      final int MAX_GUESS = 5;

      int[] guesses = new int[MAX_GUESS];

      for (int i = 0; i < guesses.length; i++) {

         do {

            try {
               guesses[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
            }

            catch (NumberFormatException e) {
               guesses[i] = 0;
            }

            if (guesses[i] < MIN_GUESS || guesses[i] > MAX_GUESS) {
               JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
            }

         } while (guesses[i] < MIN_GUESS || guesses[i] > MAX_GUESS);

      }
      return guesses;
   }

在main方法中,由于我们现在从getUserGuesses()返回一个整数数组,我们将数据存储为整数数组。

int[] guesses = getUserGuesses();

现在您的猜测数组在您的程序更灵活的范围内,您可以决定如何执行逻辑。寻找你最初尝试实现的东西,它看起来像这样:

int[] guesses = getUserGuesses();

for (int i=0; i<guesses.length; i++)
    if (isGuessCorrect(randomNum, guesses[i]))
        count++;

另外 - 只是看了一些其他函数..似乎有一些逻辑错误。

displayResult(5);

打印:

Out of 5 tries, you guessed 5 correct. You don't have any supernatural powers. Sorry!

这可以通过将OR语句更改为AND语句来解决:

   public static void displayResult(int count) {
      count = 5;
      if (count >= 0 && count <= 1) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou don’t have any supernatural powers. Sorry!");
      }
      else if (count >= 2 && count <= 3) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou might be good. Try again another time.");
      }
      else if (count >= 4 && count <= 5) {
         JOptionPane.showMessageDialog(null, 
            "Out of 5 tries, you guessed " + count + " correct.\nYou’re hired! When can you start?");
      }
   }

答案 2 :(得分:0)

public static void main(String[] args) {

  final int MIN_NUM = 1;
  final int MAX_NUM = 5;
  int count = 0;         //count is set to 0

  int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
  int guess = getUserGuess();
  boolean determineMatch = isGuessCorrect(randomNum, guess);        
  if (determineMatch == true) {
     count++;      //count set to 1
  }        
  displayResult(count);    //every time displayResult is called with count = 1
}

你的计数变量只增加一次到值1,所以你的displayResult()每次显示你只有一个猜对了