Friday, 15 April 2011

java - Recalling an Int from a method? -


I need to return an int to use 4 random fraction points (any repeated) for the main method so that I can not use string methods for the rest of my work. I want to print it random number.

It is what I have:

  public static zero main (string [] args) {int generatedSecretNumber; System.out.print ("Random number is:" + software generated;); } Public static int generateSecretNumber (int w, int x, int y, int z, int secretNumber) {random race = new random (); W = ran.nextInt (); X = Run Next (); Y = ran. NnextInt (); Z = ran.nextInt (); While (w == x || w == y || w == z) {w = ran.exit (); } While (x == w || x == y || x == z) {x = ran.nextInt (); } While (y == w || y == x || y == z) {y = Run. Nxit (); } While (z == w || z == x || z == y) {z = ran.exit (); } If (w! = X & x! = Y & y! = Z) {secretNumber = w + x + y + z; } Secret return number; }}    

In your code, you really never have your generSecretNumber Calling () Method, you're declaring a variable then instead. Remove the announcement in your head, and change the printing line to System.out.print ("random number is:" + created number ("++"); .

Next, Code> generateSecretNumber () must not have any argument in the method, because it determines what it is going to do entirely on its own, and no external data is required because logic remains , You need to declare it as int w, x, y, z; .

Other , You are generating a random integer without the upper bound is not exactly what you want, instead, your upper limit for rand.nextInt () calls must be 10, resulting in Rand.nextInt (10) . It can choose a random integer anywhere from 0 to 9.

And finally, you return the amount of digits instead of the actual four digit number Instead, return the sum of each of their points to their place For example, the fourth digit should be w * 1000 .

Example of the result:

  public class RandUniqueFourDigits {public static zero major (String) [] Args) {System.out.println ("Random number is:" + Creator number ()); } Public static int generateSecretNumber () {random rand = new random (); Int w = 0, x = 0, y = 0, z = 0; // generate each digit until all of them are unique (w == x || w == y || w == z || x == y || x == z || y == z) { W = Rand.nextInt (10); X = rand.nextInt (10); Y = RAND Xxit (10); Z = rand.nextInt (10); } // Generate each digit until they are all exclusive = rand.nextInt (10); Do x = rand.nextInt (10); While (x == w) // add them to an integer and return return w * 1000 + x * 100 + y * 10 + z; }}   

for a more efficient loop (each number is reproduced only when your initial code should be resized), this time with a completely loop Replace: w = Rand NXIT (10); Do x = rand.nextInt (10); Whereas (x == w); Do y = rand.nextInt (10); Whereas (y == w || y == x); Do z = rand.nextInt (10); While (z == w || z == x || z == y); Results in

:

  public class RandUniqueFourDigits {public static zero major (string [] args) {System.out.println (GenerateSecretNumber ()); } Public static int generateSecretNumber () {random rand = new random (); Int w = 0, x = 0, y = 0, z = 0; // Generate each digit until they are all exclusive = rand.nextInt (10); Do x = rand.nextInt (10); Whereas (x == w); Do y = rand.nextInt (10); Whereas (y == w || y == x); Do z = rand.nextInt (10); While (z == w || z == x || z == y); // Give them an integer and return return w * 1000 + x * 100 + y * 10 + z; }}    

No comments:

Post a Comment