| Author | Message |
Grapes
494 posts |
#209840 2008-03-22 23:15 GMT |
|
I have this program that loops through once you click yes at the end of it. when you click no the output comes up as the average of your input.
In other words.. First loop through I put in 80, it asks me if I want to loop again, I say yes then I put in 70. This time I click no so it gives me an output of the average of 70 and 80. I'm guessing I need an array for this program.. But I just can't seem to figure it out.. Any suggestions? |
|
Freedom
495 posts |
#209841 2008-03-22 23:24 GMT |
|
Use an ArrayList ;)
List<Integer> numberList = new ArrayList<Integer>(); When you click yes and add a number, have this: numberList.add(value); You won't need to cast the int as an Integer because Java automatically autoboxes (converts primitives to Objects). When you click no, try this: int total = 0; for (int i : numberList) { total += i; } int average = total / numberList.size(); Hopefully that helps |
|
DawnsAwaken
521 posts |
#209842 2008-03-23 00:10 GMT |
|
try this let for no u assume as 0
int no,sum=0,cnt=0; while(1) { no= number u r typing if (no!=0) { sum=sum+no ; cnt=cnt+1; } else { break; } } //end of while avg=sum/cnt; //display avg } |
|
oddperson
470 posts |
#209843 2008-03-23 00:24 GMT |
|
A good way to solve computer programs is to plan them out before you begin coding. This gives you the time and space to make mistakes and get it straight where you are going- like a route map so you dont get lost.
Your program needs to: - accept values, - store values (the numbers read in, the total so far, and a count of how many values have been read in so far) - loop and process the values - output the average Here is some sample pseudocode that should give you an idea of what to do here Begin Program initialize inputPrompt = "no" initialize total = 0 initialize count = 0 intialize average = 0.0 Output prompt Yes / No //these are your buttons While buttonClick not equal to "No" //this is the loop Output prompt "enter a number" accept and store input number //this is your input count = count + 1 //this is your processing total = input + total average = total / count Output prompt Yes / No //the user is prompted with the buttons again End While output average //output End Program You would use an array if you know how many numbers the user will enter, if its just 2 then just have 2 variables (if you need to keep a record of what they were). You would use an arraylist if you dont know how many numbers the user will enter because it will expand to accomodate them, although it will not store primitive values like int. I think this is just a procedural programming problem you have been set anyway. |
|
> 1 <
presentation candidates onenote outputs secure answer linux shelves implementation dedication hacker projector unit design viruses happen twelve checkbook xbox study link define canon law unsuspecting works industry securities students multifunction generator account attendees zafi key nerve amazon report model letters planning launching mind method landlin represents button photo natural dongle distribution
Categories: IT CD Duplication Video Game Testing Text To Speech Adware & Spyware Nintendo Wii Computers Web Conferencing Identity Carpal Tunnel Operating Systems Playstation 3 Technical Analysis Data Recovery Broadband Computer Games Internet Safety CD Duplication Satellite Radio PC Security Voice Over IP Healthy Computing The Nintendo Wii Identity Theft Bluetooth Data Recovery Projectors Printers Fiber Optics Better Health for Computer Users Broadband





