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!✓
Answers
Answered by
4
Required 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 ☑.
Attachments:
Answered by
1
Answer:
Refer to the attachment
Explanation:
Hope it helps
Attachments:
Similar questions