-5

I have this code

 public int luckyNumbers() {
    for (int i = 1; i <= 3; i++) {
    }
    return (int) (Math.random() * 10);
}

And when i call it in my main class i only get one number. Where I made mistake? That is my method in class. And yeah, i want to generate 3 random numbers with for loop in range 1 - 10. I found some answers but not in Java and not with for loop. Sorry for asking

  • 5
    Welcome to SO! There's nothing in your `for` loop. – ggorlen Jan 24 '19 at 20:33
  • your function returns one number. – OldProgrammer Jan 24 '19 at 20:33
  • Can you help me then? My for loop is bad writed? – Marko Filipovic Jan 24 '19 at 20:34
  • 1
    Even if you put the statement in your loop, you can only ever return *one* item. Did you intend to *print* those values instead? – Makoto Jan 24 '19 at 20:34
  • How i can fix that? – Marko Filipovic Jan 24 '19 at 20:34
  • My program need to work like this: User input one number from 1 - 10 and my program generate 3 random numbers, and if user input same number he will get bonus. :D – Marko Filipovic Jan 24 '19 at 20:35
  • move the `for` loop to `main`...and inside `for` loop call `luckyNumbers()` (3 times) – xerx593 Jan 24 '19 at 20:38
  • 1
    You wrote a method that returns **one** value. What did you intend that method to do, given your statement of 3 random numbers, but only one return value? Did you want it to return an array of 3 numbers? A `List` of 3 numbers? It is *your* method, so *you* need to decide what the method is supposed to do. – Andreas Jan 24 '19 at 20:39
  • My method just need to generate 3 random numbers in range 1 - 10. Thats all, at the end i just chek which number user input and if user input same number he will get bonus. – Marko Filipovic Jan 24 '19 at 20:39
  • Should generated numbers be unique like 1,2,3 or can they repeat like 1,1,1? – Pshemo Jan 24 '19 at 20:46
  • No numbers has to be unique. – Marko Filipovic Jan 24 '19 at 20:52
  • Your response looks like it can be interpreted as Yes or No, depending on if we place comma between `No[,] numbers`. So to avoid any confusion simply say if `1,1,1` is among correct possible results. – Pshemo Jan 24 '19 at 21:11
  • You are quite complicated. I just need to create a method that will generate 3 random numbers(no duplicates) in range of 1-10. That is all. With for loop outside main class. – Marko Filipovic Jan 24 '19 at 21:15
  • Possibly related: [Generating Unique Random Numbers in Java](https://stackoverflow.com/q/8115722) – Pshemo Jan 24 '19 at 21:24

2 Answers2

2

To explain why your code doesn't return 3 values, whenever you call your method, the for loop runs 3 times but does nothing as there is no code inside the loop. And the return statement calculates a random value and returns that value. So in effect, when you call your luckyNumbers() method once, only one value is returned.

To generate 3 random numbers according to your idea by using a for loop, you can do something like:

public void luckyNumbers() {
    for (int i = 1; i <= 3; i++) {
    //Print random number here
    System.out.print((Math.random() * 10));
    }
}

Another simple way would be to call this luckyNumbers() method n number of times to return n random number from another method.

If your goal is to return 3 lucky numbers at a time, then add the generated random number to an integer array or ArrayList basing on your requirement and return that.

L Dorado
  • 51
  • 3
  • I already tried with `public void` but when I need to check equals program send me error "void type not allowed here" ... When i try `p.luckyNumber()==u.getUserLucyNumber();` – Marko Filipovic Jan 24 '19 at 20:54
1

LuckyNumbers is ok so far, but it gives numbers [0..9], so + 1:;

class Test {

  public int luckyNumbers() {
   return (int) (Math.random() * 10) + 1;
  }

..and in the main method typically (with System.out.println()):

  public static void main(String... args) {
    Test testObj = new Test();
    for (int i = 1; i <= 3; i++) {
      System.out.println(testObj.luckyNumbers());
    }
  }
}

EDIT:

Voila (not in main method, but then (why not!?) void):

public void luckyNumbers() {
  for (int i = 1; i <= 3; i++) {
    System.out.println((int) (Math.random() * 10) + 1);
  }
}
public static void main(String... args) {
   new Test().luckyNumbers();
}

EDIT2:

Voila, 3 numbers [1-10]:

public int[] luckyNumbers() {
 return new int[] {
   Math.random() * 10) + 1,
   Math.random() * 10) + 1, 
   Math.random() * 10) + 1
 };
}

EDIT3 (drawLuckyUsers method):

  • Input: a List of all users, with individual lucky number [1-10]
  • Output: a List of winning users

public java.util.List<User> drawLuckyUsers(java.util.List<User> allUsers) {
   //helper structure to map all users by luckyNumber
   java.util.Map<Integer, List<User>> helper = new java.util.TreeMap<>();
   for (User u : allUsers) {
      if(helper.conatins(u.getLuckyNumber())) {
         helper.get(u.getLuckyNumber()).add(u);
      } else {
         List<User> tmp = new ArrayList<>(2);
         tmp.add(u);
         helper.put(u.getLuckyNumber(), tmp);
      }
   }
   java.util.List<User> winner = new java.util.ArrayList<>();
   for(int i = 0; i < 3; i++) {
       winner.addAll(helper.get(luckyNumbers()));
   }
   return winner;
}

with.. luckNumbers() v1:

public int luckyNumbers() {
  return (int) (Math.random() * 10) + 1;
}
xerx593
  • 12,237
  • 5
  • 33
  • 64
  • it's not OK, it still returns one `int` – Andrew Tobilko Jan 24 '19 at 20:43
  • There is no chance to make that work outside my main class? – Marko Filipovic Jan 24 '19 at 20:45
  • ...yea, but the scope of the question was not to "change a public method signature", but "where i made mistake" ...an "it" "returns" 3 numbers (try it!) – xerx593 Jan 24 '19 at 20:46
  • My variables are typed on English but my personal text is on another language, so I dont know can be that even usefull, because I think if I put here more code that will make more usefull. I will try to explain here in detail what I need and in which class's. I have PC class and User class. In my User class i have getters and setters, and User need to input number from 1-10 so he can get bonus. And in PC class i want to create method that will give me 3 random numbers from 1-10 and at the end i just need to check if user input same number so he can get bonus. I hope you now understand – Marko Filipovic Jan 24 '19 at 20:51
  • I get an error when put `public void`. I cant check equals with my user lucky number – Marko Filipovic Jan 24 '19 at 20:55
  • @xerx593 How I can contact you so not spam here anymore – Marko Filipovic Jan 24 '19 at 21:21
  • This is bigger problem than i thought, lol – Marko Filipovic Jan 24 '19 at 21:24