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
Does anyone know the answers?
frez [133]

Answer: nah bruh sorry

Explanation:

4 0
3 years ago
Question 2 of 5
goblinko [34]
B. Find one or two co-workers that you can complain to.
3 0
2 years ago
A bastion host allows the firewall to connect to the internal network and the perimeter network.TrueFalse
Delicious77 [7]

Answer: True

Explanation:

 Yes, the given statement is true, as the bastion host basically require two individual firewall that connect with the internal network and perimeter network by using the perimeter configuration.

Perimeter network basically isolate the the bastion host from the internal network. Bastion host is the serve as the host that connect with the external world. As, perimeter network is one of the type of the firewall network system.

Therefore, the internal network and the perimeter network basically connect with the firewall by using bastion host.

3 0
2 years ago
Who all likes fortnite
Debora [2.8K]

Answer:

my brother

Explanation:

5 0
3 years ago
Read 2 more answers
What are the main purposes of regulatory policies? Check all that apply.
zhenek [66]
Hey there,
<span>- To make business competitive
- To limit and prevent monopolies
- To place regulations on prices.

Hope this helps ;))

~Top
</span>
5 0
3 years ago
Read 2 more answers
Other questions:
  • The eastern front was longer than other fronts of the war true or false
    7·2 answers
  • The amount of data produced worldwide is increasing by ___% each year.
    5·1 answer
  • A server, also called a(n) _______________, operating system is a multiuser operating system because it controls a central compu
    6·1 answer
  • To defragment a disk means to ____. slow it down, diagnose problems with it, reorganize it, repair it
    8·1 answer
  • What effect does the interconnectedness of modern media have on society?
    14·2 answers
  • There are two main advantages to using multiple threads in a process: 1) Less work involved in creating a new thread rather than
    7·1 answer
  • Which domain indicates that a website is sponsored by a university or college?
    12·1 answer
  • Write a program that prompts the user to enter the area of the flat cardboard. The program then outputs the length and width of
    15·1 answer
  • Define a new class Fighter that inherits from Spaceship. Add a variable property weapon that defaults to an empty string and a v
    14·1 answer
  • Assert statements are a tool programmers employ to help them debug their code more efficiently.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!