473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Please help with quick program

I have a program that I have for homework and I tried to figure it out, but I have spent a lot of time on it. Please help me. The problem i'm having is that it is including the 0 in the sum, product and count. I'm not sure what to do. Anyway here is the code...
By the way, thanks. (I tried to add comments to help you understand what I have some things there for)
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class Compute 
  4. {
  5.     public static void main(String[] args) 
  6.     {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int count = 0;    //counts numbers to create average later
  11.         int rSum = 0;     //running sum
  12.         int rProd = 1;    //running product
  13.         int number = 1;   //initializes variable
  14.         double average;   //average of numbers input
  15.  
  16.         System.out.println("Enter numbers. Use 0 to end input");
  17.         while (number != 0)
  18.         {
  19.             number = scanner.nextInt();
  20.             rSum = rSum + number;
  21.             rProd = rProd * number;
  22.             count++;
  23.         }
  24.         average = (double) rSum / count; //makes variables double
  25.         System.out.println("Sum = " + rSum);
  26.         System.out.println("Product = " + rProd);
  27.         System.out.println("Average = " + average);
  28.     }
  29. }
  30.  
Mar 24 '07 #1
1 1007
Ganon11
3,652 Expert 2GB
Get the first input before you enter the loop, then do all your calculations first in the loop - the last statement in the loop will be the input from the Scanner.
Mar 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
6
by: gauravkhanna | last post by:
import java.io.*; class Automation1 implements Runnable { int iterationCount=0; Thread t; FileReader fr; //FileWriter fw; BufferedReader br; ...
3
NewYorker
by: NewYorker | last post by:
Hello brothers and sisters, Please help me complete this program and get the output shown below. Here is all I have ___________________________________________________________ This...
7
by: Danielle | last post by:
All - I was asked to work on a project converting some weather data. The program used to convert the data is written in Quick Basic. I am only a novice programmer and while I can see from the...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
1
by: =?Utf-8?B?SkQ=?= | last post by:
ok, I got a windows xp home edition computer from my friend, it is a custom made one. The windows xp is an OEM product, so microsoft is willing to charge me $59 to tell me how to fix it since its...
4
by: dragony2000 | last post by:
I want to solve these questions using C# , Please !!! ************************************************************* 1- The factorial method is used frequently in probability problems. The...
7
by: pooh1119 | last post by:
hi. my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.