0

I don't know how to make the "number 2" not equals to zero, because if it equals zero, it will be an exception.

I tried to let the number 2 autogenerate because if it autogenerates, the probability of being 0 is small, but it still happened.

 number1=(int) (Math.random()*10);  //Number 1 auto generate
        number2=(int) (Math.random()*10);   //Number 2 auto generate
        System.out.println(number1+"/"+number2);
        answer=sc.nextInt();

        if(answer==(number1/number2))               
        {
            System.out.println("You are correct, you get one point!");
            points=points+1;
            System.out.println("You have "+points+"points");
        } 
        else 
        {
            System.out.println("You are not correct, you get no point! I am sorry.");
            points=points+0;
            System.out.println("You have "+points+"points");
        } 

I expected to let it not to be 0, but it did and exception pop out.

  • 1
    `if (number2 != 0) {...}` or simply do: `number2 = (int) (10 * Math.random()) + 1;` – Hovercraft Full Of Eels Oct 12 '19 at 21:33
  • Does `Math.random()` generate things like `0.05235...`? Even multiplying by 10 wouldn't work for something like that. –  Oct 12 '19 at 21:33
  • 1
    `Math.random` generates a random `double` in the range from `[0,1)`. If you take something from range `[0,1)` and multiply it by 10, your new range is `[0, 10)`. If you want to make a random number from 1 to 10, you actually need to do something like: `number2=(int) (Math.random()*10+1);`, which will move the range to `[1, 11)` and be casted down to `[1, 10]` as integers. – Avi Oct 12 '19 at 21:33

0 Answers0