Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:37:19+00:00 2026-06-08T22:37:19+00:00

This is my code import java.util.*; import java.io.*; public class eCheck10A { public static

  • 0

This is my code

import java.util.*;
import java.io.*;

public class eCheck10A
{
public static void main(String[] args)
{
  PrintStream out = System.out;
  Scanner in = new Scanner(System.in);

  out.print("Enter your integers");
  out.println("Negative = sentinel");

  List<Integer> aList = new ArrayList<Integer>();



  for (int n1 = in.nextInt(); n1 > 0; n1 = in.nextInt())
  {
          if(n1 < 0)
          {
          break;
          }
  }


 }
}

if i want to take all the numbers that I enter for n1, and average them out, how do i refer to all these numbers? I am going to put them in the IF statement, so if a negative number is entered, the program stops and posts their average.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:37:21+00:00Added an answer on June 8, 2026 at 10:37 pm

    This is the pseudocode you need to do this task (pseudo-code since it looks suspiciously like homework/classwork and you’ll become a better developer if you nut out the implementation yourself).

    Because you don’t need the numbers themselves to work out the average, there’s no point in storing them. The average is defined as the sum of all numbers divided by their count, so that’s all you need to remember. Something like this should suffice:

    total = 0
    count = 0
    n1 = get_next_number()
    while n1 >= 0:
        total = total + n1
        count = count + 1
        n1 = get_next_number()
    if count == 0:
        print "No numbers were entered.
    else:
        print "Average is ", (total / count)
    

    A couple of other points I’ll mention. As it stands now, your for statement will exit at the first non-positive number (<= 0), making the if superfluous.

    In addition, you probably want any zeros to be included in the average: the average of {1,2,3} = 2 is not the same as the average of {1,2,3,0,0,0} = 1.

    You can do this in the for statement itself with something like:

    for (int n1 = in.nextInt(); n1 >= 0; n1 = in.nextInt())
    

    and then you don’t need the if/break bit inside the loop at all, similar to my provided pseudo-code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: import java.util.Scanner; public class Arrays { public static void main(String[] args)
I have this code: import java.util.Scanner; public class Example { public static void main(String[]
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
import java.util.ArrayList; import java.util.Random; public class Generator{ //9352141NTBG1223 public static void main(String[] args) {
Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
Getting error with the following code: import java.util.Scanner; public class FahrenheitToCelsius{ public static void
what is wrong in this code? import java.io.IOException; import java.util.*; public class char_digit {
this is the code: import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Vector; public class
this is my code: import java.util.Calendar; import java.util.Locale; public class test { /** *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.