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
The term drive app is used to describe applications stored on a computer true or false
Volgvan
Hello <span>Areyano7475
</span>

Question: T<span>he term drive app is used to describe applications stored on a computer true or false


Answer: False


Hope this helps
-Chris</span>
7 0
3 years ago
Read 2 more answers
An exclusion contract A. gives a firm the right to be the exclusive provider of a good in a particular market. B. is a form of e
irina1246 [14]

Answer:

D. All of the above

7 0
3 years ago
How can getchar function be used to read multicharacter strings?​
alex41 [277]

69696969696969969696969696966969696969696V69696969696969969696969696966969696969696VVVV696969696969699696969696969669696969696966969696969696996969696969696696969696969669696969696969969696969696966969696969696V69696969696969969696969696966969696969696V6969696969696996969696969696696969696969669696969696969969696969696966969696969696V69696969696969969696969696966969696969696VVVV696969696969699696969696969669696969696966969696969696996969696969696696969696969669696969696969969696969696966969696969696V69696969696969969696969696966969696969696V6969696969696996969696969696696969696969669696969696969969696969696966969696969696V69696969696969969696969696966969696969696VVVV696969696969699696969696969669696969696966969696969696996969696969696696969696969669696969696969969696969696966969696969696V69696969696969969696969696966969696969696V69696969696969969696969696966969696969696

5 0
3 years ago
Draw the Abstract Syntax Trees for the following statements and represent them in text form. i) 1+2+3 ii) 6÷3×4+3
MArishka [77]

Answer:

your a legend you can do it just pleave

Explanation:

:)

5 0
3 years ago
The labels on the tab buttons on the Driver tab of the Windows Device are Driver Details, Update Driver, Roll Back Driver, Unins
cricket20 [7]

Answer:

Disable.

Explanation:

The labels on the tab buttons on the Driver tab of the Windows Device are Driver Details, Update Driver, Roll Back Driver, Uninstall, and Disable. The aforementioned labels associated with the driver tab of a Windows device are basically the tools which can be used to perform specific functions or tasks on a particular device driver, as well as generate information about them.

<em>For instance, Uninstall button is an action which involves the process of completely removing a particular device driver in Windows. </em>

8 0
3 years ago
Other questions:
  • Define a function begins_with_line that consumes a string and returns a boolean indicating whether the string begins with a dash
    15·1 answer
  • Which type of software-generated problems can indicate that a software bug is causing a memory error?
    11·1 answer
  • When parking on hills or an unlevel surface, make sure your_____is
    15·2 answers
  • Danica is creating a flyer for her cookies that she will sell during her school fair. She wants to add a registered
    7·1 answer
  • What is the definition of posture<br>​
    7·1 answer
  • Why is computer called information processing maching???​
    6·2 answers
  • Climatologist use weather balloons, stethoscopes, and satellites true or false
    14·1 answer
  • Coding with Loops Worksheet
    13·2 answers
  • Which best compares portrait and landscape orientations?
    7·1 answer
  • What is the output for this program?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!