• Post Reply Bookmark Topic Watch Topic
  • New Topic

Student grade average program.  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm not new to Java I understand the basics I just have not got the cfhance to spend much time on it lately I know abit but I'm not an expert or even intermediate with Java so I'm trying to make a program where the teacher enters the grade of each student and its stored in a variable the only problem is if I use a for loop the variable in which the result will be stired will be overwrite by the last repitition of the loop,I think the idea would be a nested loop but I have no idea how I would go about using it in this situation,

could someone help me and explain why its done the way you think it should be done,Thanks so much really appreciate all the help =)

here is my java code.

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam, Welcome to the Ranch!

You can UseCodeTags (←click on that link) to format code that you post. That makes it easier for others to read it. Use the "Preview" button to see what your post will look like before you actually post it. That will also allow you to see the line numbers in your code formatted with Code Tags and you can reference specific line numbers that way.

I'll put the code tags in for you this time.

Thanks

Edit: next time, highlight your code first before you click on the "Code" button.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nested for-loop won't help you here, nor is it appropriate for your problem. You either need an array to store multiple grades or you can just calculate the end result as you go. You can't, however, keep using just the grade variable. You will need another variable to keep track of the results you have calculated so far, like a running total. These kinds of variables are called "accumulators" because they are used to accumulate the results of multiple calculations, such as those that are done in a loop.

Example:

Here the variable sum is an accumulator -- it keeps track of the running total of all the numbers that have been processed so far in the loop. When the loop exits, sum will contain the result of adding all the numbers from 1 to 100, which should be equal to 5050.

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How long are you wanting to store these grades? If it's something you want stored indefinitely then I think you'd have to write them to a text file. With the exception of constants, if I'm not mistaken, data in a program only lasts while the program is running.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Barrick wrote:How long are you wanting to store these grades? If it's something you want stored indefinitely then I think you'd have to write them to a text file. With the exception of constants, if I'm not mistaken, data in a program only lasts while the program is running.

That's certainly something Adam might want to consider later but I'm not sure he's quite ready to take that step yet. Let's see how he works out this immediate problem first.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:A nested for-loop won't help you here, nor is it appropriate for your problem. You either need an array to store multiple grades or you can just calculate the end result as you go. You can't, however, keep using just the grade variable. You will need another variable to keep track of the results you have calculated so far, like a running total. These kinds of variables are called "accumulators" because they are used to accumulate the results of multiple calculations, such as those that are done in a loop.

Example:

Here the variable sum is an accumulator -- it keeps track of the running total of all the numbers that have been processed so far in the loop. When the loop exits, sum will contain the result of adding all the numbers from 1 to 100, which should be equal to 5050.






Thanks so much for the help =) yes thats how my text book actually solved the problem too but I just wanted to see if I could make the program without looking at the solution,yes I was also thinking that too an array to store each variable of the grade in it but how would I assign each variable eg grade[0] grade [1] grade [2] in a for loop while incrementing athe counter aswell
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're counting in a loop where the variable i takes the values from 0, 1, 2, ..., 9 and you want to access elements grade[0], grade[1], grade[2], ..., grade[9], how do you think you would do that? Can you see a pattern? You can use i in any way and any number of times inside the loop. And you can use other variables, too. There's practically no restrictions to what you can do inside a loop. Well, there are but the restrictions are kind of common sense things to keep you out of trouble.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:If you're counting in a loop where the variable i takes the values from 0, 1, 2, ..., 9 and you want to access elements grade[0], grade[1], grade[2], ..., grade[9], how do you think you would do that? Can you see a pattern? You can use i in any way and any number of times inside the loop. And you can use other variables, too. There's practically no restrictions to what you can do inside a loop. Well, there are but the restrictions are kind of common sense things to keep you out of trouble.



thanks for the reply =)

but I was also looking how to populate the array with a for loop I learned this 2 years ago in college but can't seem to remember it,I thought it would be pretty easy to find a tutorial online but there's actually nothing =(,the part I don't know is how to actually assign them.


 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Junilu Lacar wrote:If you're counting in a loop where the variable i takes the values from 0, 1, 2, ..., 9 and you want to access elements grade[0], grade[1], grade[2], ..., grade[9], how do you think you would do that? Can you see a pattern? You can use i in any way and any number of times inside the loop. And you can use other variables, too. There's practically no restrictions to what you can do inside a loop. Well, there are but the restrictions are kind of common sense things to keep you out of trouble.



thanks for the reply =)

but I was also looking how to populate the array with a for loop I learned this 2 years ago in college but can't seem to remember it,I thought it would be pretty easy to find a tutorial online but there's actually nothing =(,the part I don't know is how to actually assign them.




I'm not to this point in Java yet but I seem to recall that in a for loop working with arrays you have to do num.length - 1. If I'm correct num.lenght would return 10 which would be 11 counting from zero.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An arrays indexes are numbered from 0 to array.length-1
So if the Array is 10 elements long, then they are numbered from 0 to 9.
If you refer to array element 10, that is the 11th element in the array, so the length is at least 11.

The standard template for looping through an array is:


You loop as long as i is less than the length (and thus a valid array index).

>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!