如何将随机整数转换为在Java中可用于使用和比较的变量?

huangapple go评论95阅读模式
英文:

How do I make a random int into a variable I can use and compare in Java?

问题

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class diceGame {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. diceGame test = new diceGame();
  7. System.out.println("Number of rolls?: ");
  8. int numRoll = scan.nextInt();
  9. System.out.println("Guess the number/most common number: ");
  10. int guess = scan.nextInt();
  11. Random rand = new Random();
  12. int[] roll = new int[numRoll];
  13. for (int i = 0; i < roll.length; i++) {
  14. roll[i] = rand.nextInt(6) + 1;
  15. }
  16. for (int i = 0; i < roll.length; i++) {
  17. System.out.print(roll[i] + ", ");
  18. }
  19. boolean correctGuess = false;
  20. for (int i = 0; i < roll.length; i++) {
  21. if (guess == roll[i]) {
  22. correctGuess = true;
  23. break;
  24. }
  25. }
  26. if (correctGuess) {
  27. System.out.println("Good job, you guessed correctly!");
  28. } else {
  29. System.out.println("You did not guess correctly.");
  30. }
  31. }
  32. }
英文:

its not great code I know but I need help at line 30 with if (guess == roll) im trying to make it if you guess the number right it will say you win and if not it will not say you win.

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class diceGame
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner scan = new Scanner(System.in);
  8. diceGame test = new diceGame();
  9. System.out.println(&quot;Number of rolls?: &quot;);
  10. int numRoll = scan.nextInt();
  11. System.out.println(&quot;Gues the number/most common number: &quot;);
  12. int guess = scan.nextInt();
  13. Random rand = new Random();
  14. for(int i = 0; i &lt; roll.length; i++)
  15. {
  16. roll[i] = rand.nextInt(6)+1;
  17. }
  18. for(int i = 0; i &lt; roll.length; i++)
  19. {
  20. System.out.print(roll[i] + &quot;, &quot;);
  21. }
  22. if (guess == roll)
  23. {
  24. System.out.println(&quot;Good job you guess correct&quot;);
  25. }
  26. else
  27. {
  28. System.out.println(&quot;You did not guess correct&quot;);
  29. }
  30. }
  31. }

答案1

得分: 0

如果你想在n次掷骰子后猜测出出现频率最高的数字,你可以将每个掷骰子的结果的计数更新到一个数组中,而不是将每次掷骰子的结果存储到数组中:

  1. for(int i = 0; i < roll.length; i++)
  2. {
  3. roll[rand.nextInt(6)]++; // 存储每个骰子的计数
  4. }

要找出你猜测的最频繁的点数是否正确,找到数组中的最大值(最常掷出的数字):

  1. int maxIdx = 0;
  2. for(int i = 0; i < roll.length; i++)
  3. {
  4. if(roll[i] > roll[maxIdx])
  5. maxIdx = i;
  6. }

要将你的猜测与最常见的数字进行比较:

  1. if (guess == maxIdx+1) // +1 以补偿数组的起始索引
  2. {
  3. System.out.println("恭喜你猜对了");
  4. }
  5. else
  6. {
  7. System.out.println("你没有猜对");
  8. }
英文:

If you would like to guess the number with the highest frequency after n number of rolls, you can update the count of each roll into an array instead of storing the roll outcome of each roll into the array:

  1. for(int i = 0; i &lt; roll.length; i++)
  2. {
  3. roll[rand.nextInt(6)]++; //store count of each roll
  4. }

To find out if you guessed the most frequency roll, find the maximum value (most commonly rolled number) of the array:

  1. int maxIdx = 0;
  2. for(int i = 0; i &lt; roll.length; i++)
  3. {
  4. if(roll[i] &gt; roll[maxIdx])
  5. maxIdx = i;
  6. }

To compare your guess against the most common number:

  1. if (guess == maxIdx+1) //+1 to maxIdx to offset array start index
  2. {
  3. System.out.println(&quot;Good job you guess correct&quot;);
  4. }
  5. else
  6. {
  7. System.out.println(&quot;You did not guess correct&quot;);
  8. }

huangapple
  • 本文由 发表于 2020年9月30日 11:42:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64130518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定