Computer Science Canada

Loops errors

Author:  slider203 [ Wed Mar 03, 2010 7:46 pm ]
Post subject:  Loops errors

Hello I have 2 porgrams that I can find the error with

the first

class Loops1
{
public static void main (string args [])
{
System.out.println ("Loops1")
int b = 0;
int MAX1 = 13;
int increment = 2;
for i = 1; i <= MAX1; i+= increment)
{
System.out.println (i);
b = i;
}
System.out.println ("After the loop b = "+b+".");
}
}

The second program is:

class Loops2
{
public static void main (string args [])
{
System.out.println ("Loops2")
int c=1;
do
{
System.out.print (c+" ");
c-=1;
}
while (c<5);
System.out.println ("\nAfter the loop c = "c+".");
}
}

Any help is greatly appreciated.

Author:  DemonWasp [ Wed Mar 03, 2010 8:05 pm ]
Post subject:  RE:Loops errors

In the first one, you're missing an open bracket after for.

In the second one, you have an upper bound on c, but c is decreasing.

In both cases, it should be String[] args, not String args[].

Author:  TheGuardian001 [ Wed Mar 03, 2010 8:46 pm ]
Post subject:  Re: RE:Loops errors

DemonWasp @ Wed Mar 03, 2010 8:05 pm wrote:

In both cases, it should be String[] args, not String args[].


Java actually accepts either one, although type[] name does make more logical sense (to me at least).

Author:  DemonWasp [ Thu Mar 04, 2010 12:03 pm ]
Post subject:  RE:Loops errors

Ah, right. Maybe I'm confusing that with C/C++ (they do make that distinction, right?).

Author:  Barbarrosa [ Thu Mar 04, 2010 11:38 pm ]
Post subject:  Re: Loops errors

You also seem to be missing semicolons after:
Java:

System.out.println ("Loops1")

and
Java:

System.out.println ("Loops2")



These should be:
Java:

System.out.println ("Loops1");

and
Java:

System.out.println ("Loops2");

respectively.


: