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 computer has best software
Kruka [31]
Dell computers have pretty good software.
4 0
3 years ago
String word = “Awesome”;
Ilia_Sergeevich [38]
I don’t understand this question
7 0
3 years ago
Read 2 more answers
Students enrolled in a digital classroom participate in discussions and take field trips with classmates. watch instructional vi
krek1111 [17]

Answer:b

Explanation:gszrxewzgdtxherhzre

7 0
3 years ago
Computer science student jones has been assigned a project on how to set up sniffer. What just he keep in mind as part of the pr
noname [10]

In the case above, what just comes to  mind as part of the process is option d.

<h3>What is sniffer?</h3>

A sniffer is known to be a kind of a software or hardware tool that gives room for a person to be able to “sniff” or look through one's internet traffic in real time, getting  all the data flowing to and from a person's computer.

Therefore, In the case above, what just comes to  mind as part of the process is option d.

Learn more about sniffer from

brainly.com/question/14265770

#SPJ1

3 0
2 years ago
People often delete or read email based upon the subject line alone select one:
erica [24]
The statement people often delete or read email based upon the subject line alone select one is TRUE. Based on the subject or topic of the email, they will judge if it is worthy to be read or deleted.
5 0
3 years ago
Read 2 more answers
Other questions:
  • You need to access the registry on your windows 10 computer how do you do this
    9·1 answer
  • Reading for subject content follows the same steps as reading for entertainment. True or false.
    10·2 answers
  • What are three reasons teens might start drinking alcohol??
    7·2 answers
  • Write a program that asks the user to enter a whole number then outputs a 0 if the number is even or a 1 if the number is odd. (
    14·1 answer
  • Briefly summarize two examples of cybercrime stories.<br> quick pleaseeee
    10·1 answer
  • What tool should you use to look for information on a network?
    14·2 answers
  • The computer mouse are used for
    15·2 answers
  • How does computer number system play a Vital role in a computer calculation. ​
    8·1 answer
  • QUESTION 10
    11·1 answer
  • For computer forensics, ____________ is the task of collecting digital evidence from electronic media.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!