Question

In: Computer Science

JAVA: Compute the average of a list of user-entered integers representing rolls of two dice. The...

JAVA:

Compute the average of a list of user-entered integers representing rolls of two dice. The list ends when 0 is entered. Integers must be in the range 2 to 12 (inclusive); integers outside the range don't contribute to the average. Output the average, and the number of valid and invalid integers (excluding the ending 0). If only 0 is entered, output 0. The output may be a floating-point value. Ex: If the user enters 8 12 13 0, the output is:

Average: 10
Valid: 2
Invalid: 1

Hints:

  • Use a while loop with expression (userInt != 0).

  • Read the user's input into userInt before the loop, and also at the end of the loop body.

  • In the loop body, use an if-else to distinguish integers in the range and integers outside the range.

  • For integers in the range, keep track of the sum, and number of integers. For integers outside the range, just keep track of the number.

  • Use a cast to get a floating-point output: (double) sum / num .

  • Whenever dividing, be sure to handle the case of the denominator being 0.

Solutions

Expert Solution

SOLUTION-
I have solve the problem in Java code with screenshot for easy understanding :)

CODE-

//java code

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);
       int sum = 0;
       int validCount = 0, invalidCount = 0;
       float avg;
       int userInt = scnr.nextInt();
       while (userInt != 0) {
           if (userInt >= 2 && userInt <= 12) {
               sum += userInt;
               validCount++;
           } else {
               invalidCount++;
           }
           userInt = scnr.nextInt();
       }
       if (validCount == 0) {
           System.out.println("Average: 0");
       } else {
           avg = (float) sum / validCount;
           System.out.println("Average: " + avg + " Valid: " + validCount + " Invalid: " + invalidCount);
       }
       scnr.close();
   }
}

SCREENSHOT-

Sample output 1:

Sample output 2:

Sample output 3:


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

In a dice game a player first rolls two dice. If the two numbers are l...
In a dice game a player first rolls two dice. If the two numbers are l ≤ m then he wins if the third roll n has l≤n≤m. In words if he rolls a 5 and a 2, then he wins if the third roll is 2,3,4, or 5, while if he rolls two 4’s his only chance of winning is to roll another 4. What is the probability he wins?
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Write a Java application that prompts the user for an age. If the age entered is...
Write a Java application that prompts the user for an age. If the age entered is greater or equal to 65, display the statement "Age is greater than or equal to 65"; otherwise display the message "Age is less than 65". If the age entered is less than 18; display the statement "This person is a minor"; otherwise display the message "This person can legally vote. Do not create a class for this application. The code can be created in...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die has six faces, which contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. (i) If the sum is 2 or 10 on the first throw, the player wins. (ii) If the sum is 3, 7 or 12 on the first throw, the player loses. (iii)...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing a list of integers. The following public methods are provided: ? IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0. ? void randomize()—fills the list with random integers between 1 and 100, inclusive. ? void print()—prints the array elements and indices ? int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm. Returns...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls...
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls have a sum that exceeds 6? at least 7 of the rolls have a sum that is even? exactly three rolls have a sum that equals 5?
(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT