Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2007, 03:24 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default i really need help
hey, this is my first post on this forum. I'll get right to the point. I'm creating a program that does a bunch of math operations such as Law of Cosines, Quadratic Formula, etc. I'm working on an tool that computes the Average of a specified amount of numbers given by the user. What i have so far, is an integer array that contains the number of numbers that the user can choose. I've also constructed a new integer array for the actual numbers. Now I'm trying to use a for loop in order for it to display "What is number 1?", "What is number 2?", etc. It prints that with no problem, but when i try to get it to have the values inputted and averaged, what it's doing is just dividing the last inputted number. This is all very unclear so take a look at the code:

Scanner scan = new Scanner(System.in);
System.out.print("How many numbers? ");
int x = scan.nextInt(); //numofnums
int[] numOfNums = {2, 3, 4, 5, 6, 7, 8, 9};
int z = numOfNums[x-2]; //index of array
int average = 0;

for(int y = 1; y < (z+1); y++)
{
System.out.print("What is number " + y + "? ");
Scanner scan2 = new Scanner(System.in);
int[] nums = new int[8];
average = scan2.nextInt() / x;
}

System.out.println(average);



-------------------
Can someone please help me figure out how to make it so that the numbers the user inputs are taken into account and averaged.

Last edited by gibsonrocker800; 11-14-2007 at 03:30 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-14-2007, 05:33 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
Rep Power: 0
JT4NK3D is on a distinguished road
Default
if its a problem with the scanner, theres a good text class i got from a tutorial called TextIO that prints, gets input and other text related things with no problem.. just click the link TextIO.java at Javanotes 5.0, Section 2.4 -- Text Input and Output
if its a problem with sumthing else i dont know.. btw just compile TextIO, get it in same folder and use either the TextIO.getlnInt(); method or TextIO.getlnDouble(); method or TextIO.getlnBoolean(); method theres on for all primitive types and string i think... use what you want in it
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 06:27 AM
Member
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0
blackstone is on a distinguished road
Default
The reason it is only dividing the last number entered is because of the last line in your for loop: average = scan2.nextInt() / x;

All that is doing is getting another number from the user and dividing it by the total number of integers that there should be. What you need to do is be adding up all the numbers entered by the user and then, outside of the for loop, divide that total by the total number of integers to find the average.

And just to satisfy my own curiosity, what are you using the numOfNums array for? I don't quite understand that...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-15-2007, 12:57 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default
i figured it out. and the reason why i'm setting up an array for the numOfNums is because i'm trying to learn how to put arrays to use, i could have easily set this program up with if statements or something, but i want to advance in my logic skills.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +2. The time now is 08:27 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org