Question
Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop
Create a program using the Random class and While loops in Java.
The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is....
Teach comment the last time I did it:
*Your comments are way too long and should be written on multiple lines. *You did not follow the directions given at all. *You were supposed to print out the random number and the partial sum each iteration. Ie: The Random number for the number 4 generated is: 52 The partial sum is: 228 *You were supposed to print out on the last iteration the the final sum is and the last random number generated. ie: The Last random number is: 57 The final sum is: 285 *You have variables declared and initialized that are never used, and not needed *You used the wildcard import, which was not allowed Totally incorrect.
\_()_/
My ans:
//Generate a random number from 50 through 100, Add 5 random numbers generated. Then tells the user the number generated, how many nubmers have been generated thus far, and the sume so far. On the last iteration, the program should tell the user the final total is. //Jan 21, 2020
import java.util.*;
public class RandSum { public static void main(String[]args) { int sum=0, count=50, num; Random randNum=new Random(); int i=0; while(i<5) { num=randNum.nextInt(51)+count; System.out.println(num); sum+=num; i++; } System.out.println("The sum is "+sum); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started