How do I write code in Java for "A spinner for a game has four possible options: blue, red, yellow, green. The chance of landing on each option is equal. Simulate using the spinner 15 times and reporting the resulting color."?

Respuesta :

What you need to do is to generate a random number between 1 and 4(or0 and 3) 15 times. Each time you generate a number, you figure out which number it is and keep track of how many times you’ve seen that number. At the end, print out how many of each number you got.

Good luck!

Following are the program that calculates value by using the switch statement:

Program:

public class Spinner // defining a class Spinner

{

   public static void main(String[] args) //defining the main method

   {

       for (int roll = 1; roll <= 15; roll++) //defining a for loop that counts roll number value

       {

           int arrow = (int) (Math.random() * 4) + 1;//defining an integer variable arrow that holds its value

           System.out.print("Spin #"+roll+" --> ");//print roll number value

           switch (arrow)//defining switch statement that checks roll number value

           {

               case 1: //for case 1

                   System.out.println("Blue"); //print message

                   break;//using break keyword

               case 2: //for case 2

                   System.out.println("Red"); //print message

                   break;//using break keyword

               case 3://for case 3            

                   System.out.println("Yellow"); //print message

                   break;//using break keyword

               case 4: //for case 4

                   System.out.println("Green"); //print message

                   break;//using break keyword

           }

       }

   }

}

Program Explanation:

  • Defining a class Spinner.
  • Defining the method main.
  • Inside the method, a for loop is declared that uses a roll variable as integer that counts/holds the value randomly in the arrow variable.
  • In the next step, a print method is declared that prints the value and uses a switch statement that checks and prints its calculated value.

Output:

Please find the attached file.

Find out more about the switch statement here:

brainly.com/question/14617875

Ver imagen codiepienagoya