Creación de un generador de números aleatorios que toma una entrada de límite superior y los números que generará el usuario e imprime los resultados.
Por alguna razón, mi programa (que compila), no reconoce el argumento de límite como el segundo argumento y sale por mi verificación de argumentos.
Cualquier ayuda sería apreciada en mi comprensión de cómo solucionar esto y lo que me puede estar perdiendo.
¡Gracias!
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); } }}
Intente solicitar solo la entrada del usuario, no use argumentos. Y no use dos escáneres diferentes, simplemente llame a numGen.nextInt(); dos veces.