随机数生成器在给定范围时始终返回 0。

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

Random number generator always returning 0 when given a range

问题

我正在制作一个猜数字游戏,在这个游戏中,用户会设定一个最大数字,然后这个数字会被用作随机数生成器的范围,但每次我测试我的程序时,它都输出 0。我不太清楚为什么。

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {

public static Scanner scan = new Scanner (System.in);
public static Random random = new Random();

public static int answer;
public static int maxNumber;
public static int minNumber = 0;

public static void main (String [] args ){

  System.out.println("Welcome to the Guessing Game.");
  System.out.println("Enter the maximum number.");
  int maxNumber = scan.nextInt();
  System.out.println(answer); // 测试后删除
} 
 public static int newGame(Random random) {
  int randNumber;
  int range = maxNumber - minNumber;
  randNumber = random.nextInt(range);
  answer = randNumber;
     return answer;
}
英文:

I'm making a guessing game where the user sets the maximum number and that number is used for the range for the random number generator, but every time I test my program it outputs 0. I'm not sure why.

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {

public static Scanner scan = new Scanner (System.in);
public static Random random = new Random();

public static int answer;  
public static int maxNumber;  
public static int minNumber = 0;              

public static void main (String [] args ){                     
                            
  System.out.println("Welcome to the Guessing Game.");
  System.out.println("Enter the maximum number.");
  int maxNumber = scan.nextInt(); 
  System.out.println(answer); //Remove after testing 
} 
 public static int newGame(Random random) {  
  int randNumber;
  int range = maxNumber - minNumber; 
  randNumber = random.nextInt(range);                                 
  answer = randNumber;     
     return answer;     
}                    

答案1

得分: 1

你没有调用public static int newGame(Random random)函数。在Java中,整数的默认值是0。

你没有调用这个函数,所以它没有改变answer的值。

另外,如果你不想为此使用额外的方法,你可以使用以下代码:

public static void main(String[] args) {
    System.out.println("欢迎来到猜数字游戏。");
    System.out.println("请输入最大数字。");
    int maxNumber = scan.nextInt();
    answer = new Random().nextInt(maxNumber - minNumber);
    System.out.println(answer); // 测试后移除
}

或者如果你想使用这个方法:

public static void main(String[] args) {
    System.out.println("欢迎来到猜数字游戏。");
    System.out.println("请输入最大数字。");
    maxNumber = scan.nextInt();
    answer = newGame(random);
    System.out.println(answer); // 测试后移除
}

public static int newGame(Random random) {
    int range = maxNumber - minNumber;
    return random.nextInt(range);
}
英文:

You did not call the public static int newGame(Random random) function. In java the default value of an integer is 0.

You did not call the function, so it did not change the value of answer.

Also if you don't want an extra method for this you can use the following:

public static void main (String [] args ){                     
                            
  System.out.println("Welcome to the Guessing Game.");
  System.out.println("Enter the maximum number.");
  int maxNumber = scan.nextInt(); 
  answer = new Random().nextInt(maxNumber - minNumber);
  System.out.println(answer); //Remove after testing 
} 

Or if you want to use the method:

public static void main (String [] args ){                     
                            
  System.out.println("Welcome to the Guessing Game.");
  System.out.println("Enter the maximum number.");
  maxNumber = scan.nextInt(); 
  answer = newGame(random);
  System.out.println(answer); //Remove after testing 
} 

public static int newGame(Random random) {  
  int range = maxNumber - minNumber; 
  return random.nextInt(range);      
} 

答案2

得分: 0

你在main函数中没有调用过newGame函数!
你可能需要在main函数中这样做:

System.out.println(newGame(random));

或者:

answer = newGame(random);
System.out.println(answer);

你可能需要查看这个函数:

public static int randNum(int min, int max) {
    Random rand = new Random();
    int val = Math.abs(rand.nextInt());
    val = (val % (max - min + 1)) + min;
    
    return val;
}

这个函数用于在minmax之间随机生成一个非负数。如果你只需要任何非负数,你可以将min设置为0,或者直接从公式中省略min

英文:

You have NOT called the function newGame in main!
You may have to do this in main:

System.out.println(newGame(random));

OR:

answer = newGame(random);
System.out.println(answer);

You may want to look at this function:

public static int randNum(int min, int max)	{
		Random rand = new Random();
		int val = Math.abs(rand.nextInt());
		val = (val % (max - min + 1)) + min;
		
		return val;
	}

This function is to randomize a non-negative number between min and max. You can let it min = 0 or just eliminate min from the formula if you just need any non-negative number.

答案3

得分: 0

scan.nextInt() 之后添加这一行代码:

answer = newGame(random);

另外,在你的 newGame 方法中有一个错误。将:

answer = randNumber;

改为:

answer = randNumber + minNumber;
英文:

Add this line after scan.nextInt():

answer = newGame(random);

Also, there's a bug in your newGame method. Change:

answer = randNumber; 

to

answer = randNumber + minNumber;

huangapple
  • 本文由 发表于 2020年10月22日 07:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64473142.html
匿名

发表评论

匿名网友

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

确定