构建一个随机数生成器,它接受上边界输入,并由用户生成数字并打印结果。
无论出于何种原因,我的程序(编译)都不会将边界参数识别为第二个参数,并且根据我的参数检查退出。
在理解如何解决这个问题以及我可能缺少的内容时,我们将不胜感激。
谢谢!
import java.util.Random;
import java.util.Scanner;
public class RandNumGen{
public static void main(String[] args){
// intro
System.out.println("*****************************************");
System.out.println("*Welcome to the Random Number Generator!*");
System.out.println("* Discussion 7: Michael Stadnicki *");
System.out.println("*****************************************");
// ask user to input numbers to be generated
System.out.println("Please enter how many random numbers you would like generated!");
Scanner numGen = new Scanner(System.in);
int randomNum = numGen.nextInt();
System.out.println("We will generate " + randomNum +" random numbers!");
// ask user to input out boundary for the random numbers
System.out.println("Please enter the boundary for our random numbers to be generated!");
Scanner boundGen = new Scanner(System.in);
int randomBound = boundGen.nextInt();
System.out.println("We will use " + randomBound +" as our boundary for generation!");
// build our random number generator
Random rNum = new Random();
// check our arguments
if (args.length == 2) {
randomNum = Integer.parseInt(args[0]);
randomBound = Integer.parseInt(args[1]);
} else {
System.out.println("You are required to have 2 arguments on the command line!");
System.exit(0);
}
// print our random numbers generated based on above bounds and numbers
for (int i = 0; i <randomNum; i++) {
int numOutput = rNum.nextInt(randomBound + 1);
System.out.println("Your random numbers are: " + numOutput);
}
}
}
答案 0 :(得分:0)
尝试仅询问用户输入,不要使用args。并且不要使用两种不同的扫描仪,只需拨打电话即可 numGen.nextInt(); 两次。