Question

Create a program using the Random class and While loops in Java. The program should generate...

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);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

//Jan 21, 2020

import java.util.*;

public class RandSum
{
public static void main(String[]args)
{
int sum=0, count=50, num;//variables
Random randNum=new Random();//random class
int i=0;//variable i defined
  
while(i<5)//while loop
{
num=randNum.nextInt(51)+count;//generate random
sum+=num;//generate sum
System.out.println("number "+(i+1)+" generated is: "+num);//print num
System.out.println("The partial sum is: "+sum);//print partial
i++;
}
System.out.println("The sum is "+sum);
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Create a program using the Random class and While loops in Java. The program should generate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • [Using Python] Create a program that uses import random and while loops to generate one random...

    [Using Python] Create a program that uses import random and while loops to generate one random question, from a list of 5, for each repeated run. Report the number of the correct answers after all five questions have been answered. Your 5 questions should be a simple math problem, like "what is 7-5?"

  • [Using Python] Create a program to generate one random question, from a list of 5, for...

    [Using Python] Create a program to generate one random question, from a list of 5, for each repeated run. Report the number of the correct answers after all five questions have been answered. Your 5 questions should be a simple math problem, like "what is 5-4?" use a while loop based on the number of questions use a counter variable to count the number of questions asked e.g. questionCount +=1 user a counter variable to keep track of the correct...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • For this assignment, write a program that will generate random numbers in the range of 50-100...

    For this assignment, write a program that will generate random numbers in the range of 50-100 and count how many fall into particular ranges. Processing: The program should first seed the random number generator. This is done one time in the program. Use srand(0). Next, generate and display a series of 10 random numbers between 50 and 100. There are several ways to ensure that a random number falls between 50 and 100. One possibility is to use the following...

  • programming language Write a program that will generate 5 random numbers between 20 and 80 and...

    programming language Write a program that will generate 5 random numbers between 20 and 80 and assign each to a variable of datatype double. You should use a random number generator seeded with time Using a series of if statements, determine which of the 5 variables contains the smallest value generated Print all five random numbers to the screen and print out the value that you determined was the smallest. All values should be printed with zero decimal places. 2.

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT