-2

I need to print the circumference with Math.random() * Math.Pi; but i'm doing something wrong or missing something. Each random generated number equals the radius of the circle. My idea was to calculate Pi in the getRandomNumberInRange method but when I do, I get error:

Bad operand for type double

import java.util.Random;
import java.util.Scanner;

    final static double PI = 3.141592564;
    static Scanner sc = new Scanner(System.in);

        public static void main(String[] args) {

    //ask the player to enter a number less than or equal to 18 and higher to 9.

                            System.out.println(" Please enter a number less than or equal to 18 and above 9: ");
                            int random = sc.nextInt ();

                            //send error message if bad input
                              if (random < 9 || random > 18) {
                System.out.println(" Error. Unauthorized entry . You need to enter a number less than or equal to 18 and above 9 ");
            } else

                          //If the answer is yes , generate nine different random numbers from 0.
                            for (int i = 0; i < 9; i++) {

                                    double surface = PI * (random * 2);

                System.out.println(getRandomNumberInRange(9, 18) + " : " + " The circumference is : " + surface );
            }}

The method called:

private static int getRandomNumberInRange(int min, int max) {

        Random r = new Random();

                return r.nextInt((max - min) + 1) + min;
    }
Jane Nice
  • 97
  • 1
  • 1
  • 8
  • Assuming `surface` is supposed to be the circumference, you're generating a random number but you're not using that random number to compute the circumference. You're computing the circumference with a variable named `random`, but that variable isn't a random number. I really do not know what you're trying to accomplish. – ajb Jan 03 '16 at 06:22
  • Only the nine generated numbers must be used to compute the circumference. The user enter a number between 9 and 18. Say 10. This input will then generate 9 random numbers. Each number generated equals a different radius. – Jane Nice Jan 03 '16 at 06:24
  • I can't tell where the error is coming from. The compiler will tell you what line number the error is on. Please figure out what line that number refers to, and let us know. (And don't just tell us the line number. That won't help us. Best is to edit your question and add some text to the line saying "THE ERROR IS HERE" or something like that. – ajb Jan 03 '16 at 06:25
  • So what happens with the user's input? Say they enter 11. What effect does that have on the program? – ajb Jan 03 '16 at 06:26
  • The input is used to generate the numbers. – Jane Nice Jan 03 '16 at 06:28
  • You need to be more specific than that. **How** is the input used to generate the numbers? Please keep in mind that I am not a mind reader. – ajb Jan 03 '16 at 06:29

1 Answers1

1

You call getRandomNumberInRange() in the for loop, but don't assign it to anything, or use it. This is probably closer to what you want:

        for (int i = 0; i < 9; i++) {
            int r2 = getRandomNumberInRange(9, 18);
            double surface = PI * (r2 * 2);

            System.out.println(r2 + " : " + " The circumference is : " + surface);
        }
Ian Mc
  • 5,656
  • 4
  • 18
  • 25