Computer Science Canada Program Help! Please!Important! |
| Author: | Caceres [ Fri Jun 30, 2006 10:13 pm ] | ||
| Post subject: | Program Help! Please!Important! | ||
Hello. I was wondering if someone could help me with making this program. I'm not sure of the codes (the info I put into the editor). I used Crimson Editor. And so far..I have..
The assignement is" Write a program that will calculate the average for a set of marks. The user enters a -1 when there are no more marks to enter. The program will count the number of marks entered and calculate the average. The output could look like this : You entered 24 marks and the average is 73.8. " I'm a newbie programmer (literally an extreme newb) and i Don't know how to make this program. Thank you. ~Caceres |
|||
| Author: | TheFerret [ Sat Jul 01, 2006 1:19 am ] |
| Post subject: | |
We are not going to write your program, so don't expect that... But, you have to find what number people input and then add it onto a total and add 1 to how many numbers people enter and if they enter -1 exit... After that, you output total/number... |
|
| Author: | cool dude [ Sat Jul 01, 2006 9:43 am ] | ||||||
| Post subject: | |||||||
wow i recently just started learning java myself and even i can make such a simple program. its really simple 1) make a variable count and every time a mark is inputed make count increase by 1
2)everytime a new mark is entered make a sum variable and add the new mark to the old mark and store it in the sum variable. 3)make a average variable and to get average divide the sum of all the marks by the count (how many marks in total)
4)then all you have left is to display the output
|
|||||||
| Author: | wtd [ Sat Jul 01, 2006 9:52 am ] | ||
| Post subject: | |||
cool dude wrote: wow i recently just started learning java myself and even i can make such a simple program. its really simple
1) make a variable count and every time a mark is inputed make count increase by 1
This is not quite right. |
|||
| Author: | cool dude [ Sat Jul 01, 2006 11:00 am ] | ||||
| Post subject: | |||||
wtd wrote: cool dude wrote: wow i recently just started learning java myself and even i can make such a simple program. its really simple
1) make a variable count and every time a mark is inputed make count increase by 1
This is not quite right. hmmm... i think its because i can't declare it on the same line like that because i have to first initialize it. right?
|
|||||
| Author: | wtd [ Sat Jul 01, 2006 11:51 am ] |
| Post subject: | |
Yes. Variables should always be initialized before they're used. It just happens that in Java that initialization should be explicit. |
|
| Author: | Token [ Sat Jul 01, 2006 12:20 pm ] | ||
| Post subject: | |||
Just a shortcut
increments by one |
|||
| Author: | wtd [ Sat Jul 01, 2006 12:39 pm ] | ||
| Post subject: | |||
Or more generally:
|
|||
| Author: | Caceres [ Sat Jul 01, 2006 1:35 pm ] |
| Post subject: | |
public class ass25c { public static void main(String[] args) { KeyboardReader reader = new KeyboardReader(); int count = 0; int stop = -1; System.out.println("Please enter marks\nWhen you've entered them all, type -1:"); for(count=0; count==stop; count=count++) {; } } } My information still doesn't work. :S |
|
| Author: | cool dude [ Sat Jul 01, 2006 1:42 pm ] |
| Post subject: | |
you need to learn the basics of java! firstly why are u using a counted loop to see if the user pressed -1 to exit??? thats incorrect. if u read my post i told you use a conditional loop also you don't even know how to make a counted loop! although you don't need it. read my post carefully, because i tell you step be step wat to write! P.S. obviously your information still doesn't work and i really hope you see why! |
|
| Author: | Caceres [ Sat Jul 01, 2006 4:09 pm ] |
| Post subject: | |
I don't know whta a condition loop is. This is my 5th day with Java. All i've learned is "if", "while" and "for". |
|
| Author: | wtd [ Sat Jul 01, 2006 4:19 pm ] | ||||||||||
| Post subject: | |||||||||||
A "counting loop" is a "conditional loop." Just a fairly predictable loop. Basically, in a loop you start with some initial state. You then proceed to do something. At some point you check to see if a condition holds true. If it does not, then you exit the loop. At the end of each iteration of the loop, you have some kind of update. A simple loop:
Includes all four parts. The initial state is:
The test is:
The update is:
And the thing you're doing is:
|
|||||||||||
| Author: | cool dude [ Sun Jul 02, 2006 10:14 am ] | ||
| Post subject: | |||
instead of your counted loop which uses "for" use something like this:
as you can see the loop will keep looping while the mark is not -1. as soon as the user enters the mark -1 it will exit the loop. now inside this loop you put your code. |
|||
| Author: | Aziz [ Wed Jul 12, 2006 2:01 pm ] |
| Post subject: | |
It seems you barely even know the basics of programming, so I suggest a full-blown tutorial: The Java Tutorial Follow that, young friend. Then go to "Learning the Java Language" on the same site. |
|
| Author: | Aziz [ Wed Jul 12, 2006 2:04 pm ] |
| Post subject: | |
ADD: (sorry, I can't seem to remember how to edit, or why it won't let me) Or check out this tutorial here: http://www.compsci.ca/v2/viewtopic.php?t=9576 |
|