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
Free 35 points!!!
Black_prince [1.1K]

Answer:

free points?? why does it have question then

Explanation:

5 0
3 years ago
Read 2 more answers
Hey guys, I don’t have a problem for you but If anyone knows do you still pass your grade level if you failed 1 class in the las
Hoochie [10]

Answer:

I think you have to retake it:():

3 0
2 years ago
Read 2 more answers
Mathematical computations by a computer are faster than your quickest mathematical computations because the top speed of a neura
Tema [17]

Answer:

3 million

Explanation:

According to my research on information technology, I can say that based on the information provided within the question the top speed of a neural impulse is about 3 million times slower than the speed of electricity through the wired circuitry in a computer. Neurons have been calculated as firing roughly 200 times per second, while a computer processing unit processes at about 600 million instructions per second.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

6 0
3 years ago
In the MOV instruction both operands i.e. source andthe destination cannot be
nekit [7.7K]

Answer:

an immediate.

Explanation:

It is because of the instruction encoding and decoding.Intel which make these instructions decided not include instructions which provide no real advantage. MOV segment register is one of them because segment register need not to be changed very often.This saves the space in instruction encoding.

3 0
4 years ago
Read 2 more answers
What is an example of a transition effectl
yawa3891 [41]
Well try finding out what transition effect means 
8 0
3 years ago
Other questions:
  • How can we set the color of a text that acts as a link in a web page​
    10·2 answers
  • Is bit stuffing necessary in the control or address field in theHDLC protocol? why?
    13·1 answer
  • After clicking the Start button on your computer screen desktop, what option would you then select to examine system components
    10·2 answers
  • Write a program that utilizes the concept of conditional execution, takes a string as input, and: prints the sentence "Yes - Spa
    8·1 answer
  • After adding an email account, how do you switch accounts to send a message?
    10·2 answers
  • Charles would like to move his internet browser window so that he can see his desktop.He should
    5·1 answer
  • Examples of analog computer
    8·1 answer
  • Select the correct answer from each drop-down menu. A company is recruiting for a web designer. What kind of candidate should th
    14·2 answers
  • TO Cloud
    10·1 answer
  • You find a picture of a famous department store online that would be great to include in a project of yours. What should you do
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!