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
What is the missing line of code? >>> >>> math.sqrt(16) 4.0 >>> math.ceil(5.20) 6
Blababa [14]

Answer:

A math.pow reference

Explanation:

3 0
2 years ago
Read 2 more answers
I.d 9934607467<br> p.a.s.s 54321<br><br> P.L.E.A.S.E J.O.I.N. Z.O.0.M. M.E.E.T.I.N.G
dusya [7]

Answer:

You could just go to social medIa to say j.o.i.n z.o.o.m not brainly mate

7 0
3 years ago
When listing columns in the select list, what should you use to separate the columns?
Gala2k [10]
The answer is commas.  <span>When listing columns in the select list, commas should be used to separate the columns.</span>
6 0
3 years ago
(HELP ASAP) Jenae is helping her dad create a blog for his garden club. Which tool will she use?
ser-zykov [4K]
She would make use of JavaScript if she wants to make it interactive, CSS if she wants to design it, Text editor obviously for coding the blog and a web browser for the result hope this helps
7 0
2 years ago
Read 2 more answers
Universal Containers (UC) uses a custom object called Vendor. The Vendor custom object has a Master-Detail relationship with the
Rudik [331]

Answer:

The account record comprises of Invoice roll up summary fields.

Explanation:

The possible reason that this change was not permitted was that the account record comprises of Invoice roll up summary fields.

Invoice roll up summary fields: A roll-up summary field computes values from associated records, for example those in a linked list. a person or someone ca design a roll-up summary field to show a value in a master record by building  the values of fields in a particular record.

The detail record must be associated to the master through a master-detail relationship. for instance, you want to show the sum of invoice amounts for all linked custom object records in an account’s Invoices related list. you can show the whole or sum in a custom account field refereed to as Total Invoice Amount.

4 0
3 years ago
Other questions:
  • _________ are represented using diamonds linked withparticipant ETs
    6·1 answer
  • One megabyte is approximately __________.
    7·2 answers
  • What is the system of phonographic disc recordings paired with a projector called?
    9·1 answer
  • ____ are programs that need to be attached to other files to install themselves on computers without the users’ knowledge or per
    5·1 answer
  • Which of the following best defines Monte Carlo simulation?a. It is a tool for building statistical models that characterize rel
    11·1 answer
  • Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, s
    11·1 answer
  • A healthcare organization received notification that a hospital employee’s laptop that contained PHI was inadvertently left at a
    14·1 answer
  • I'm getting pretty desperate plz help me, I'll give brainiest, and ill make a free question worth 100 points this is for coding
    10·1 answer
  • Assume you are working for the laboratory that is developing the control system for the LISAsatellites. Your job is to develop a
    9·1 answer
  • Why Use LinkedIn AI Automation Tools to Grow Your Sales Pipeline?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!