1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
marin [14]
4 years ago
7

Write code that prompts for three integers, averages them, and prints the average. Make your code robust against invalid input;

if the user types a non-number, re-prompt with the same prompt message. (You may want to look at the getInt method discussed in Chapter 5. You can call that method in your solution if you like.) Here is an example dialogue: Type an integer: 5 Type an integer: 2 Type an integer: 17 Average: 8.0
Computers and Technology
1 answer:
MArishka [77]4 years ago
6 0

Answer:

The program to this question can be given as:

Program:

import java.util.*;  //import package for user input.

public class Main  //define class-main

{

   public static void main (String ar [])  //define main function

   {

       try  //try block

       {

           int a,b,c,sum=0;  //declare variable

           double average;

           Scanner ob= new Scanner(System.in);  //create Scanner class Object

           System.out.print("Enter first number :");  //message.

           a = ob.nextInt();  //input number.

           System.out.print("Enter second number :");

           b = ob.nextInt();

           System.out.print("Enter third number :");

           c = ob.nextInt();

           //print numbers

           System.out.print("\nFirst number:"+a);

           System.out.print("\nSecondnumber:"+b);

           System.out.print("\nThird number:"+c);

           sum =sum+a+b+c;    //Sum

           average = sum/3;  //Average

           System.out.print("\nAverage:" + average);  //print average

       }  

       catch (Exception e) //catch block.

       {

          System.out.print("Enter number only..!");  //message

       }

   }

}

Output:

Enter first number 5

Enter second number 2

Enter third number 17

First number: 5  

Second number: 2  

Third number: 17  

Average: 8.0

Explanation:

In the above program firstly we import packages for user input. Then we declare the main class. In the main class, we define the main function in the main function we use the exception handling. In the exception handling, we use try and catch block. In the try block firstly we declare variables then create a scanner class object and take input from the user. After user input we add all numbers into the sum variable then we take average in the average variable and print all values. If the user doesn't input a number then we use the catch block in this block it prints a simple message that is "Enter number only..!"

You might be interested in
The function takes two string parameters; the first is the name of a file and the second is text. Complete the function to appen
castortr0y [4]

Answer:

The code for the function is given below in Python language

Explanation:

def append_string_to_file(filename, text):// function takes the filename // and text  to append

f = open(filename, 'a')

f.write(text) //function part that writes the text

f.close() //closing the file using the explicit close function

3 0
4 years ago
In this chapter, you saw an example of how to write an algorithm that determines whether a number is even or odd. Write a progra
vagabundo [1.1K]

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Program starts here

public class RandomOddEve {

/** Main Method */

public static void main(String[] args) {

int[] nums = new int[100]; // Declare an array of 100 integers

// Store the counts of 100 random numbers

for (int i = 1; i <= 100; i++) {

nums[(int)(Math.random() * 10)]++;

}

int odd = 0, even = 0; // declare even and odd variables to 0, respectively

// Both variables will serve a counters

// Check for odd and even numbers

for(int I = 0; I<100; I++)

{

if (nums[I]%2 == 0) {// Even number.

even++;

}

else // Odd number.

{

odd++;

}

}

//.Print Results

System.out.print("Odd number = "+odd);

System.out.print("Even number = "+even);

}

8 0
4 years ago
Read 2 more answers
The first graphical browser application for using the web was netscape. <br> a. True <br> b. False
Mamont248 [21]
Hello <span>Jcece6710 
</span>

Question: <span>The first graphical browser application for using the web was netscape. True or False


Answer: True

Hope that helps
-Chris</span>
5 0
3 years ago
_______ is the protocol suite for the current Internet.
Gala2k [10]
A? not very sure sorry
8 0
3 years ago
Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an arr
Svetllana [295]

Answer:

//The Scanner class is imported to allow the program receive user input

import java.util.Scanner;

//The class Solution is defined

public class Solution {

   //The main method is defined here and signify the beginning of program execution

   public static void main(String args[]) {

       

       //Scanner object 'scan' is created to receive user input

       Scanner scan = new Scanner(System.in);

       //Prompt display to the user to enter size of array

       System.out.print("Enter the range of array: ");

       //User input is assigned to arraySize

       int arraySize = scan.nextInt();

       //userArray is initialized with arraySize as its size

       int[] userArray = new int[arraySize];

       

       //counter to count number of array element

       int count = 0;

       //while loop which continue executing till the user finish entering the array element

       while (count < arraySize){

           System.out.print("Enter each element of the array: ");

           userArray[count] = scan.nextInt();

           count++;

       }

       

       //A blank line is printed for clarity

       System.out.println();

       

       //for loop to print each element of the array on straight line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

       

       //A blank line is printed for clarity

       System.out.println();

       

       //for loop is use to reverse the array in-place

       for(int i=0; i<userArray.length/2; i++){

           int temp = userArray[i];

           userArray[i] = userArray[userArray.length -i -1];

           userArray[userArray.length -i -1] = temp;

       }

       

       //for loop to print each element of the reversed array on straight line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

     

   }

}

Explanation:

The program is commented to give detailed explanation.

The for-loop use in reversing the array works by first dividing the array into two half and exchanging the first half elements with the second half elements. The element from the first half is assigned to temp variable on each loop, then the element is replaced with the equivalent element from the second half. And the element from the second half is replaced with the value of temp.

3 0
3 years ago
Other questions:
  • Which statement is true about the elements of the interface of a presentation program? The status bar appears at the top of the
    14·2 answers
  • Suppose that a company offers quantity discounts. If up to​ 1,000 units are​ purchased, the unit price is ​$1010​; if more than​
    7·1 answer
  • Cyberlaw consists of: a. only state statutes. b. only federal statutes. c. traditional legal principles that have changed becaus
    6·1 answer
  • How can you autohide the taskbar in Windows 10?
    8·2 answers
  • Describe one type of technology that is useful in producing images from space.
    9·1 answer
  • Which of these sites would need additional investigation to check for reliability?
    8·2 answers
  • Write a program that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user’s gue
    5·1 answer
  • Almost immediately after a server migration project, employees are complaining that they can't reach the Internet. You sit down
    8·1 answer
  • Write a program that reads from the user any three points in two dimensional space: x1, y1, x2, y2, x3, y3. Assume these points
    13·1 answer
  • What is the process of adding random characters at the beginning or end of a password to generate a completely different hash ca
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!