Author |
Message |
Caceres

|
Posted: 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..
code: | import TerminalIO.KeyboardReader;
public class ass25c {
public static void main(String[] args) {
System.out.print("Please enter marks\nWhen you've entered them all, type -1.\n");
}
} |
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 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
TheFerret

|
Posted: Sat Jul 01, 2006 1:19 am Post subject: (No 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... |
|
|
|
|
 |
cool dude

|
Posted: Sat Jul 01, 2006 9:43 am Post subject: (No 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 code: | int count = count + 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) code: | int average = sum / count |
4)then all you have left is to display the output code: | system.out.println("you entered " + count + " marks and the average is " + average);
Note: make a conditional loop for when your entering marks so that you can exit the loop when user enters -1 or it will keep asking for marks.
hope that helps |
|
|
|
|
|
 |
wtd
|
Posted: Sat Jul 01, 2006 9:52 am Post subject: (No 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 code: | int count = count + 1; |
This is not quite right.  |
|
|
|
|
 |
cool dude

|
Posted: Sat Jul 01, 2006 11:00 am Post subject: (No 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 code: | int count = count + 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?
code: |
int count = 0;
count = count + 1; |
|
|
|
|
|
 |
wtd
|
Posted: Sat Jul 01, 2006 11:51 am Post subject: (No subject) |
|
|
Yes. Variables should always be initialized before they're used. It just happens that in Java that initialization should be explicit. |
|
|
|
|
 |
Token

|
Posted: Sat Jul 01, 2006 12:20 pm Post subject: (No subject) |
|
|
Just a shortcut
increments by one  |
|
|
|
|
 |
wtd
|
Posted: Sat Jul 01, 2006 12:39 pm Post subject: (No subject) |
|
|
Or more generally:
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Caceres

|
Posted: Sat Jul 01, 2006 1:35 pm Post subject: (No 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 |
|
|
|
|
 |
cool dude

|
Posted: Sat Jul 01, 2006 1:42 pm Post subject: (No 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! |
|
|
|
|
 |
Caceres

|
Posted: Sat Jul 01, 2006 4:09 pm Post subject: (No 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". |
|
|
|
|
 |
wtd
|
Posted: Sat Jul 01, 2006 4:19 pm Post subject: (No 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:
code: | for (int i = 0; i < 10; i++) {
System.out.println(i);
} |
Includes all four parts. The initial state is:
The test is:
The update is:
And the thing you're doing is:
code: | System.out.println(i); |
|
|
|
|
|
 |
cool dude

|
Posted: Sun Jul 02, 2006 10:14 am Post subject: (No subject) |
|
|
instead of your counted loop which uses "for" use something like this:
code: |
while (mark != -1) {
//code
} |
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. |
|
|
|
|
 |
Aziz

|
Posted: Wed Jul 12, 2006 2:01 pm Post subject: (No 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. |
|
|
|
|
 |
Aziz

|
|
|
|
 |
|