This is a lab project we are working on, and I am at a standstill. I am in a beginner Computer Science course, for background purposes, so please don't get too technical. Any help would be greatly appreciated. Thanks

1. A variable that counts the iterations of a loop is called a loop index or loop control variable. In the preceding examples nyear serves as an index, counting the number of years to the next millennium. This type of loop is frequently written using a for loop.
for (initialization; condition; update)
statement
Write a program controlled by two (non-nested) for loops that produces the following listing of inclusive dates, from the fifth century B.C. through the fifth century A.D.

Century 5 BC 400-499
Century 4 BC 300-399
Century 3 BC 200-299
Century 2 BC 100-199
Century 1 BC 1-99
Century 1 AD 1-99
Century 2 AD 100-199
Century 3 AD 200-299
Century 4 AD 300-399
Century 5 AD 400-499

Here is what I have; I can get the first line to come out, but having problem with the repetition down.

public class Century
{
public static void main(String[] args)
{
for (int i = 5 ; i <= 5 ; i++)
{
int years = ((i-1)*100);
int years2 = ((i*100) -1);
System.out.println("Century " + i +" "+ "BC" +" "+ years + " - " + years2);
{

{
for (int j = 1; j <=1 ; j++)
{
int year3 = ((j+1)*100);
int years4 = ((j*100) -1);
System.out.println("Century " + j +" "+ "AD" +" "+ years + " - " + years2);
{
}
}
}
}}}
}

Recommended Answers

All 15 Replies

Member Avatar for iamthwee

Hi, it looks ok.

All you need is the nested part.

Hi Iamthewee,
Thanks for the reply. But she says to use two non-nested loops, thats where I am confused.

Thanks

Member Avatar for iamthwee

Oh sorrie! I didn't see the non nested bit - and I thought using nesting was a bit strange!

The only thing I can see that is causing the problem is here:

for (int i = 5 ; i <= 5 ; i++)

shouldn't it be?

for (int i = 5 ; i > 0 ; i--)

And a problem when you try to print 1.

something times 100 is one. Is that the best way?

Thanks that did work, I just have to figure out the second loop now. Appreciate it!!

Well I have tried several different ways of doing the second part. Logically to me, anyways, this seems right, but now I get this when I run it?? Ok so what did I do wrong, is it almost not exact opposite going away from zero??

for (int j = 1; j < 6 ; j++)

Century 5 BC 400 - 499
Century 1 AD 1 - 99
Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499
Century 4 BC 300 - 399
Century 1 AD 1 - 99
Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499
Century 3 BC 200 - 299
Century 1 AD 1 - 99
Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499
Century 2 BC 100 - 199
Century 1 AD 1 - 99
Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499
Century 1 BC 0 - 99
Century 1 AD 1 - 99
Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499

Member Avatar for iamthwee

Prolly because you are not closing your braces in the for loop immediately(curly brackets)

for ( int i=0; i < 5; i++ )
{
  //do stuff
}


for ( int j = 0; j < 5; j++ )
{
  //do crap
}

Thanks, that was the problem. Going to tutor Monday. The only thing that I have totally understood so far has been programming graphics, I guess b/c I was a director over an Art Dept. at one point in time.

Member Avatar for iamthwee

Thanks, that was the problem. Going to tutor Monday. The only thing that I have totally understood so far has been programming graphics, I guess b/c I was a director over an Art Dept. at one point in time.

Don't worry, if you have any queries with graphics in java I know the answers to them too. :)

Post back if you are having problems.

What type of art dept were you head of?

Member Avatar for iamthwee

What kind of art department were you head over?

I worked at Gold Bond Inc., goldbondinc.com, we printed or still print many products, mostly lineart at 1200dpi. You can see some of the products on the website. Sorry you can only order if you are ASI certified, details. I got out of it for reasons of not being able to move up in the company, plus I had married the owner's daughter, probably a lil more than you needed to know. So, now I am starting over in Computer Engineering, Math I got so far, just need to find a way to get java thru my head. I have another problem if your willing??

Member Avatar for iamthwee

I have another problem if your willing??

shoot.

This is the second part of the same problem. I have the first part of the answer the "BC" part but my "AD" is totaly wrong??

Write the same program with a single loop for (i = -5 ; i <= 5 ; i++) and an if in the body of the loop.

I have this:
public class Date
{
public static void main(String[] args)
{
for (int i = -5 ; i <= 5 ; i++)
{
int years = ((-1-i)*100);
int years2 = ((-i*100) -1);
System.out.println("Century " + i +" " +" "+ years + " - " + years2);
{
if (i < 0)
{
String era = "BC";
}
else
{
String era = "AD";
}
}}}
}

Member Avatar for iamthwee

Also

Century 2 AD 101 - 199
Century 3 AD 201 - 299
Century 4 AD 301 - 399
Century 5 AD 401 - 499

I don't know if you have noticed. But it's still out by one. You need to give that a little more thought.

Member Avatar for iamthwee

Write the same program with a single loop for (i = -5 ; i <= 5 ; i++) and an if in the body of the loop.

try and draw a flow chart on paper.

k. will do

commented: Nice to see so happy, hard-working student :-) Peter_budo +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.