154k views
4 votes
Write a static method named printTwoDigit that accepts an integer n as a parameter and that prints a series of n randomly generated numbers. The method should use Math.random() to select numbers in the range of 10 to 19 inclusive where each number is equally likely to be chosen. After displaying each number that was produced, the method should indicate whether the number 13 was ever selected ("we saw a 13!") or not ("no 13 was seen."). You may assume that the value of n passed is at least 0.

User Bublik
by
3.2k points

1 Answer

7 votes

Answer: Following code is in java language :-

import java.lang.Math;

public class

{

public static void main(String args[])

{

printTwoDigit(6); //6 is the number of random numbers to be generated

}

public static void printTwoDigit(int n)

{

int i = 0;

int arr[]=new int[n]; //will declare a array of n size

int flag=0;

while(i<n)

{

arr[i] = 10 + (int)(Math.random()*10); //produce random number each time

System.out.println(arr[i]+" "); //print all random numbers

i++;

}

for(i=0;i<n;i++)

{

if(arr[i]==13) //to check if 13 was generated

{

flag=1;

break;

}

}

if(flag==1)

System.out.println("we saw a 13"); //this line is mentioned in question

else

System.out.println("no 13 was seen"); //this line is mentioned in question

}

}

OUTPUT :

15

11

15

19

13

19

we saw a 13

Step-by-step explanation:

  • In the above code, an array is declared with size n(which is passed as an argument to the function) to store the list of all random number produced.
  • Random() is multiplied by 10 so that it can produce a number 10 numbers starting from 0 and then 10 is added to the generated number so that minimum value generated would be 10.
  • All numbers generated are printed to console n times and stored in arr.
  • After that arr is checked for value 13 so it can print whether 13 was produced or not.
User Leo Valeriano
by
3.2k points