Uni Programing Help

Hi all im trying to convert 24 if statements into 2 for loops, This is what they look like:

Code:
        for(int counter=monthBirth;counter<monthBirth;counter++){
            monthBirth++;
            monthTotal = monthTotal+daysInMonth[monthBirth];
            
        }        
        for(int counter=month;counter<month;counter++){
            System.out.println(counter);
            month++;
            monthTotalFinal = monthTotalFinal+daysInMonth[month];
        }

However they do nothing for some reason, and no output is printed for them.... Anyone hint what im doing wrong please?
 
Code:
        int counter1 = monthBirth;
        int counter2 = month;
        
        for(int counter=counter1;counter<monthBirth;counter++){
            counter1++;
            monthTotal = monthTotal+daysInMonth[monthBirth];
            
        }        
        for(int counter=counter2;counter<month;counter++){
            System.out.println(counter);
            counter++;
            monthTotalFinal = monthTotalFinal+daysInMonth[month];
        }

Still returns nothing at all
 
jcb33 said:
Code:
        int counter1 = monthBirth;
        int counter2 = month;
        
        for(int counter=counter1;counter<monthBirth;counter++){
            counter1++;
            monthTotal = monthTotal+daysInMonth[monthBirth];
            
        }        
        for(int counter=counter2;counter<month;counter++){
            System.out.println(counter);
            counter++;
            monthTotalFinal = monthTotalFinal+daysInMonth[month];
        }

Still returns nothing at all
You start the loop with counter = month (yes, you've replaced 'month' with counter2, but counter2 = month, so it doesn't make any difference), and the loop condition is to stop as soon as (counter<month) is false. Does it really surprise you that the loop doesn't do anything?

You really need to read up on the syntax for a for() loop. To be honest, I find it shocking that you could get almost 57 percent for your last coursework and be coming up with code this poor.
 
DaveF said:
You start the loop with counter = month (yes, you've replaced 'month' with counter2, but counter2 = month, so it doesn't make any difference), and the loop condition is to stop as soon as (counter<month) is false. Does it really surprise you that the loop doesn't do anything?

You really need to read up on the syntax for a for() loop. To be honest, I find it shocking that you could get almost 57 percent for your last coursework and be coming up with code this poor.
You know, no need to be rude, for a start its my first year, and the programing we are learning is very basic, and for a second its very easy to make a mistake and not spot it!

Code:
int counter1 = monthBirth+1;
        int counter2 = month+1;
        
        for(int counter=counter1;counter<month;counter++){
            counter1++;
            monthTotal = monthTotal+daysInMonth[counter1];
            
        }        
        for(int counter=counter2;counter<monthBirth;counter++){
            System.out.println(counter);
            counter++;
            monthTotalFinal = monthTotalFinal+daysInMonth[counter2];
        }

Is what I now have, but still does not return the correct value, may end up going with the 24 if statements after all...
 
What are you actually trying to achieve with that code?

One thing that you are doing is in the second loop you're incrememnting counter twice, is that what you want to do?

If so then it makes more sense it do something like...

Code:
for(int counter=counter2;counter<monthBirth;counter+=2){

and to then get rid of the line

Code:
counter++;
 
Last edited:
jcb33 said:
You know, no need to be rude, for a start its my first year, and the programing we are learning is very basic, and for a second its very easy to make a mistake and not spot it!

He has a point though, not everyone is cut out for programming. Your code is totally crazy.. first you need to understand what you want to do before you even try implement it. If you cant think it through in pseudo code no one will be able to help.
 
this thread wouldn't have got anywhere near as many posts had it been posted in the right section.

scrap "HTML, Graphics & Programming" i say :)
 
This is a crazy left field idea, but I'm guessing you're paying a fair amount to go to whatever uni you're at, so how about you go ask a lecturer or Phd student to talk you through it. It's a much better idea than cobbling together other peoples code that you don't really get, will help you a whole lot more in the long run and I'm sure they really won't mind. Hell, even if they dock you a load of marks for getting help, (which they won't) it's the first year and doesn't really matter anyway.

As I learnt at AA, admitting you have a problem is the first step to recovery.
 
Phnom_Penh said:
Put it this way, I'll give you a magic cookie if you can find me a year that divides exactly by 4 and isn't a leap year. (century's excluded :p).

Well the "centuries excluded" bit certainly doesn't gel with what you initially said... the whole point of my post was to point out that there are years divisible by four, i.e. those that are not divisible by 400, that are not leap years.

Well done for being able to inaccurately translate pseudocode to Java though... bleedin' first-year computer science students think they know it all *grumble*
 
Last edited:
robmiller said:
Well the "centuries excluded" bit certainly doesn't gel with what you initially said... the whole point of my post was to point out that there are years divisible by four, i.e. those that are divisible by 400, that are not leap years.

Well done for being able to inaccurately translate pseudocode to Java though... bleedin' first-year computer science students think they know it all *grumble*
I hate to tell you this, but years divisible by 400 are in fact leap years. The full rule is:
wiki said:
if year mod 400 eq 0 then leap
else if year mod 100 eq 0 then no_leap
else if year mod 4 eq 0 then leap
else no_leap
 
DaveF said:
I hate to tell you this, but years divisible by 400 are in fact leap years. The full rule is:
Isn't it just that every year divisible by 4 is a leap year? Surely if there is a leap year every 4 years, then every year that is divisible by 4 is a leap year.
 
DaveF said:
I hate to tell you this, but years divisible by 400 are in fact leap years. The full rule is:

I already posted that algorithm earlier in the thread what's going on

Anyway I just missed out a "not" no big deal
 
Psyk said:
Isn't it just that every year divisible by 4 is a leap year? Surely if there is a leap year every 4 years, then every year that is divisible by 4 is a leap year.

In order to get a closer approximation, it was decided to have a leap day 97 years out of 400 rather than once every 4 years. To implement the model, it was provided that years divisible by 100 would be leap years only if they were divisible by 400 as well.
 
Psyk said:
Isn't it just that every year divisible by 4 is a leap year? Surely if there is a leap year every 4 years, then every year that is divisible by 4 is a leap year.
Problem is, there isn't a leap year every 4 years. 1896 was a leap year, but 1900 was not (even if Microsoft Excel thinks it was!).

This all happens because the length of the year means that you actually want about 97 leap years every 400 years.

Robmiller: Fair enough. Just somewhat amusing in the same post as complaining about "students think they know it all"... :-) (And I'm darn sure I've made a stupid mistake or two in this thread myself).
 
DaveF said:
Problem is, there isn't a leap year every 4 years. 1896 was a leap year, but 1900 was not (even if Microsoft Excel thinks it was!).

This all happens because the length of the year means that you actually want about 97 leap years every 400 years.

Robmiller: Fair enough. Just somewhat amusing in the same post as complaining about "students think they know it all"... :-) (And I'm darn sure I've made a stupid mistake or two in this thread myself).

The "students think they know it all" thing was rather tongue-in-cheek considering I'm a student myself—although not of a poncy subject like comp sci :p
 
Back
Top Bottom