InterviewSolution
| 1. |
WAP to roll a dice until a six comes up. Display the outcomes except when a one comes up. Also, display the number of iterations it takes.Note☞ I want program + explanation!✓ |
|
Answer» d Answer:-Question:WAP to roll a DICE until a six comes up. Display the outcomes except when a one comes up. Also, display the number of iterations it takes.Solution:Here comes the program. public CLASS Dice { public static void main(String[] args) { int count=0; while(true) { int d=(int)(Math.random()*6)+1; if(d!=1) { System.out.println("Dice rolling.. "); System.out.println("RESULT: "+d+"\n"); ++count; } if(d==6) break; } System.out.println("Total Iterations: "+count); }}Algorithm:Iterate a loop infinite times. USING random function, generate random NUMBERS between 1 to 6. Consider that they are the outcomes of a dice. Count the number of iterations taken. Display the outcomes if the result is not 1 or else skip. Terminate the loop if the outcome is six. Display the total number of iterations taken. Note: You may get different output when the given códe is executed as random function is used.Refer to the attachment for output ☑. |
|