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
How to see how many words you have on google docs?
liberstina [14]
In the menu bar, click Tools > Word Count or hit Ctrl/Cmd + Shift + C.
4 0
3 years ago
Convert the following decimal fraction to binary with a maximum of six places to the right of the binary point: 194.03125Note: n
nika2105 [10]

Answer:

11000010.000010

Explanation:

3 0
3 years ago
Which of the following is a/are question(s) that you should ask before you create an Access report?
never [62]

Answer: All the above questions should be asked

Explanation:  Access report are the reports in access which provides the summarized and formatted information display in database. The information in the database is extracted from tables.The access report requires purpose of the report before preparation and the accessing users information to be known .

It is asked to make sure about that no unauthorized access of the report can take place. Distribution of the report among the other user or client is also a major question to be asked as to keep the record of the accessing of access report..Thus, all the question mentioned in the options are correct.

5 0
3 years ago
The secure evidence locker is located at the ____.
olga55 [171]
The data recovery lab

Data recovery can be done in special rooms called data recovery labs. Data that has been salvaged, lost, or inaccessible from removable media or secondary storage can be accessed in a normal way. Recovery of this kind may be required due to the physical damage caused.

 

 

 





5 0
3 years ago
A speech why friends are important 2 min​
Gnoma [55]

Friends can challenge us, confuse us, and sometimes, we might wonder why we bother. But friendship is as important to our wellbeing as eating right and exercising. What’s more, friendships help us grow through each year of our lives.

The friends we meet in school teach us how to be patient, wait our turn, reach out, and try new hobbies. When we move into young adulthood we learn more about taking responsibility, finding a career path, and seeking out people as mentors.

As we continue into our 40s and beyond, we learn to weather the ups and downs in life, and once again friends provide a sounding board and place for us to grow. Friendship is key to our success with all our relationships and it can create a sense of purpose in our lives.

The people we bring into our lives as friends will show us how to forgive, laugh, and make conversation. The basic components of any relationship, from our marriage to our coworkers, are all founded in friendship. We learn how to interact with people because of our friends, even the ones that are opposite from us or share a different worldview.

We don’t just talk with others but learn from them. We understand the process of meeting new acquaintances and finding out what makes them tick. These people help push us out of our comfort zones while still providing a safe emotional space for us to be totally ourselves.

One of the most overlooked benefits of friendship is that it helps keep our minds and bodies strong. In fact, it’s as important to our physical health as eating well and keeping fit. A recent Harvard study concluded that having solid friendships in our life even helps promote brain health.

4 0
3 years ago
Other questions:
  • Which of the following statements best describes O*NET OnLine?
    7·1 answer
  • 1. The future of 2D animation does not include hand-drawing. (1 point)
    13·1 answer
  • Write a setInterval() function that increases the count by 1 and displays the new count in counterElement every 300 milliseconds
    12·1 answer
  • Design a database to keep data about college students, their academic advisors, the clubs they belong to, the moderators of the
    12·2 answers
  • Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters. For exampl
    13·1 answer
  • Which of the following is the definition of registration authority ( RA)?
    10·1 answer
  • • Suppose an application generates chunks of 40 bytes of data every 20 msec, and each chunk gets encapsulated in a TCP segment a
    5·1 answer
  • Explain: The decode part of fetch decode execute cycle
    9·2 answers
  • The ____ method returns an integer that represents the location of the substring within the string.
    13·1 answer
  • What mistake might you make related to changing the data in a cell that's used in a formula?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!