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 are the pros and cons of using ICT
qaws [65]

Answer:

Pros:

- Connect with friends, family in a short period of connecting time

- Faster, easier than paper

- The messages can be stored in the device for longer times, without being damaged, unlike paper files that easily get damages

- Digital communication can be done over large distances through internet and other things.

And many other pros

Cons:

- Software can be easily hacked

- Digital Communication has completelymake people become far to others. They would stop meeting face-to-face but instead of that, video call, chatting is become more popular

- Most of devices have hidden recording mechanisms that can take your information unknowingly and may be used to attack you in the future

And many other cons

Hope this helped :3

5 0
3 years ago
Which of the following statement about CAD is NOT true A. It increase the productivity of designers B. It uses computer graphics
Sedaia [141]

Answer:

D. All are true.

Explanation:

All the given statements about CAD are true.

5 0
3 years ago
Does anyone else have edge and think its not the awful?
oksano4ka [1.4K]

Answer:

yea

Explanation:

3 0
3 years ago
Read 2 more answers
This technology was developed in the 1980s and was a successful attempt to provide services for voice, video, and video traffic
sveticcg [70]

Answer:

SONET was the only one on the list created near the 1980s (1985)

Good luck!

<em>~Awwsome</em>

8 0
3 years ago
2 4.1.2 Quiz: Information Systems and Business Planning
Delicious77 [7]

Answer:

Option D

Explanation:

Complete question

A management information system is typically used by _____.

A. all levels of employees

B. company executives

C. upper-level managers

D. middle-level managers

Solution

A management information system is typically used by middle-level managers

Option D is the correct answer

Management information system (MIS) helps the middle managers to control the work across the various teams of organization.

5 0
3 years ago
Other questions:
  • What is a benefit of peer-to-peer networking?
    14·2 answers
  • (Game Design) A float is a blank space, used as a placeholder in an array<br><br> True or false
    13·1 answer
  • A user clicked an email link that led to a website than infected the workstation with a virus. The virus encrypted all the netwo
    10·1 answer
  • _____ are independent and not associated with the marketing efforts of any particular company or brand.​
    9·1 answer
  • HELPPPP PLEASE HURRY
    15·1 answer
  • During a test, it is generally best to: A. answer all questions in order B answer the essay questions first C. answer the least
    7·2 answers
  • The Curiosity Rover has recently landed on Mars and likes to send Twitter updates on its progress. If a tweet is posted 10 minut
    6·1 answer
  • Explain the concept of conditional formatting​
    9·1 answer
  • The field of ____ is concerned with the technical issues involved in information display. computer science
    14·1 answer
  • Criminal investigations are limited to finding data defined in the search ____.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!