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
Zielflug [23.3K]
3 years ago
12

Write a complete Java method that returns an integer. The method declares a Scanner object, then prompts the user for a series o

f integers in a while loop. If the user enters an integer (not a string, double, etc) then read each value entered and keep a running total (a sum). Make sure to validate that the user enters only integers. Include a sentinel value (-1) to allow the user to indicate when they are done. The method returns the total.
Computers and Technology
1 answer:
igor_vitrenko [27]3 years ago
3 0

Answer:

Here is code in java.

import java.util.*;

class Main

{

 // main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

        // print the running total of all inputs

       System.out.println("total of all the inputs is: "+fun());

       

   }catch(Exception ex){

       return;}

}

/* function to take input from user until input is -1

 and keep a running total */

public static int fun()

{

    int in;

     //scanner class object to read the input

    Scanner scr=new Scanner(System.in);

     // variable to keep running total

    int total=0;

    System.out.println("please enter an Integer:(-1 to stop)");

     // read the input first time

    in=scr.nextInt();

    while(in!=-1)

    {

     // this loop keep taking input until input is -1.

     // calculate running total of all inputs

        total=total+in;

        System.out.println("please enter an number:(-1 to stop)::");

        in=scr.nextInt();

       

    }

     // return total of all inputs

    return total;

   

}

}

Explanation:

Create an object of scanner class in the method fun_tot(),it will take input from user.In while loop it will ask user to give input and calculate the sum of all inputs.when user enter -1 then while loop breaks and the method will return the total of all inputs given by the user.

Output:

please enter an number:(-1 to stop)::23

please enter an number:(-1 to stop)::3

please enter an number:(-1 to stop)::5

please enter an number:(-1 to stop)::76

please enter an number:(-1 to stop)::12

please enter an number:(-1 to stop)::-1

total of all the inputs is: 119

You might be interested in
Entering the formula =SUM(C5:C18) in cell C19 will result in which of the following?
frozen [14]
Well the result shown in C19 will show the sum of all cells from C5 to C18:
ie: C5 + C6+ C7+ C8+ C9+ C10+ C11+ C12+ C13+ C14+ C15+ C16+ C17+ C18

:)
3 0
3 years ago
A customer reports that recently several files cannot be accessed. The service technician decides to check the hard disk status
Lyrx [107]

Answer:

Back up the user data to removable disk

Explanation:

Before you work on a computer, especially anything that has to do with files not accessible, this might need to format the system because it might either be a virus or other forms of malware. Since backup was done to a different logical partition on the disk, the first thing to do before performing any diagnostic procedures on the disk is to back up the user data to a removable disk in order not to lose the information in the system.

8 0
3 years ago
Determine the "answer" to the function according to the spreadsheet below: =SUM(A1:A3)/2
nataly862011 [7]
If this is the full question " <span>Which of the following formulas is equivalent to =SUM(A1:A3)? A. =A1+A3 B. =A1+A2+A3 B. =A2 D. =A3-A1"


The answer is </span>
<span>B. =A1+A2+A3 formulas is equivalent to =SUM(A1:A3)</span>
4 0
3 years ago
Which if the following is an example of tangible property?
lutik1710 [3]
It may have been a songlyrics
7 0
3 years ago
The smallest unit of time in music called?
zmey [24]

Answer:

Ready to help ☺️

Explanation:

A <em>tatum</em> is a feature of music that has been defined as<u> the smallest time interval</u> between notes in a rhythmic phrase.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Generally speaking, manufacturing overhead is applied to production by means of a __________ __________ __________, which is com
    11·2 answers
  • Which best explains what a credit score represents
    10·2 answers
  • Trina Hauger works for Johnson Electric as a corporate lawyer, and part of her duties are to ensure the ethical and legal use of
    13·1 answer
  • The__ key is used to group or identify a field or record within a table. (you don't need it).
    5·2 answers
  • Given an integer n and an array a of length n, your task is to apply the following mutation to an: Array a mutates into a new ar
    5·1 answer
  • Five examples of technology in community​
    7·2 answers
  • ¿Que es la energía? (una sola respuesta es la correcta)
    11·1 answer
  • What is a programming concept based on objects and data and how they relate to one another, instead of logic and actions. Hint:
    10·1 answer
  • True or False? Popular sites are always mean accurate.
    14·2 answers
  • Can somebody please help me with these few questions?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!