Java Looping problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tigger2000
    New Member
    • Feb 2007
    • 1

    Java Looping problem

    I keep getting an error when trying to compile this for Java prog class, below is the instructions for the assignment and my code.
    Loop Assignment

    Write, compile, and run a Java program that demonstrates the following:

    #1. Uses
    ach off number down to zero, except the number 3 and 33. Program must use a continue and an if statement.

    #2. Uses a while loop to count down from 30 to zero, printing each number, except 27 until the program reaches 5 and then terminates the loop and the program. Program must use a break statement.

    #3. Convert #1 to a for loop.

    #4. Convert #2 to a do-while loop.

    Include each piece of code and screen shot respectively of each program’s output in a Word document.

    Access the Loop-Assign document from Week #3. You have already written the code for the four loop problems which you will now combine into one program with four distinct methods. For this assignment you will write methods, two value returning methods and two void methods. You may choose which of the four problems are coded into which method. Each method must represent one of the 4 distinct loop problems. The methods must include:
    one value-returning method with a parameter
    one value-returning method without a parameter
    one void method with a parameter
    one void method without a parameter
    Your program must compile and run.



    //code below//

    public class LOOPmod
    {

    public static void main(String args[])
    {
    int counter = 40;

    while(counter >= 0)
    {
    if (counter % 2 == 1){
    if (counter == 33 || counter == 3){
    counter--;
    continue;}
    System.out.prin tln(counter + " ");}
    counter--;
    }
    }
    }

    public class LOOP2
    {

    public static void main(String args[])
    {
    int counter = 40;

    while(counter >= 0)
    {
    if (counter == 5) //exit
    if (counter == 27)break;
    counter--;
    System.out.prin tln(counter + " ");
    counter--;
    }
    }
    }
    public class Loop3
    {

    public static void main(String args[])
    {
    int counter = 30;

    for (counter = 30; counter <= 5; counter++){
    System.out.prin tln("*");
    {
    if (counter % 2 == 1){
    if (counter == 33 || counter == 3){
    counter--;
    continue;}
    System.out.prin tln(counter + "*");{
    counter--;
    }

    class LOOP4
    {

    public static void main(String args[])
    {
    int counter = 40;
    do
    {
    System.out.prin tln(counter + " ");
    counter = counter + 1;
    }

    while(counter >= 0);
    {
    if (counter == 5) //exit
    counter--;
    System.out.prin tln(counter + " ");
    counter--;
    }
    }
    }

    //below is the error msg that I get everytime//

    LOOPmod.java:75 : '}' expected
    }
    ^
    1 error
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Tigger2000
    I keep getting an error when trying to compile this for Java prog class, below is the instructions for the assignment and my code.
    Loop Assignment

    Write, compile, and run a Java program that demonstrates the following:

    #1. Uses
    ach off number down to zero, except the number 3 and 33. Program must use a continue and an if statement.

    #2. Uses a while loop to count down from 30 to zero, printing each number, except 27 until the program reaches 5 and then terminates the loop and the program. Program must use a break statement.

    #3. Convert #1 to a for loop.

    #4. Convert #2 to a do-while loop.

    Include each piece of code and screen shot respectively of each program’s output in a Word document.

    Access the Loop-Assign document from Week #3. You have already written the code for the four loop problems which you will now combine into one program with four distinct methods. For this assignment you will write methods, two value returning methods and two void methods. You may choose which of the four problems are coded into which method. Each method must represent one of the 4 distinct loop problems. The methods must include:
    one value-returning method with a parameter
    one value-returning method without a parameter
    one void method with a parameter
    one void method without a parameter
    Your program must compile and run.



    //code below//

    public class LOOPmod
    {

    public static void main(String args[])
    {
    int counter = 40;

    while(counter >= 0)
    {
    if (counter % 2 == 1){
    if (counter == 33 || counter == 3){
    counter--;
    continue;}
    System.out.prin tln(counter + " ");}
    counter--;
    }
    }
    }

    public class LOOP2
    {

    public static void main(String args[])
    {
    int counter = 40;

    while(counter >= 0)
    {
    if (counter == 5) //exit
    if (counter == 27)break;
    counter--;
    System.out.prin tln(counter + " ");
    counter--;
    }
    }
    }
    public class Loop3
    {

    public static void main(String args[])
    {
    int counter = 30;

    for (counter = 30; counter <= 5; counter++){
    System.out.prin tln("*");
    {
    if (counter % 2 == 1){
    if (counter == 33 || counter == 3){
    counter--;
    continue;}
    System.out.prin tln(counter + "*");{
    counter--;
    }

    class LOOP4
    {

    public static void main(String args[])
    {
    int counter = 40;
    do
    {
    System.out.prin tln(counter + " ");
    counter = counter + 1;
    }

    while(counter >= 0);
    {
    if (counter == 5) //exit
    counter--;
    System.out.prin tln(counter + " ");
    counter--;
    }
    }
    }

    //below is the error msg that I get everytime//

    LOOPmod.java:75 : '}' expected
    }
    ^
    1 error
    Indent your code and post it using code tags.

    Comment

    • rocky88
      New Member
      • Oct 2008
      • 1

      #3
      < incorrrect spoonfeeding code removed (JosAH) >

      Comment

      • karthickkuchanur
        New Member
        • Dec 2007
        • 156

        #4
        check the Paranthesis which block are open and closed correctly

        Comment

        • itsraghz
          New Member
          • Mar 2007
          • 124

          #5
          Originally posted by Tigger2000
          //below is the error msg that I get everytime//

          LOOPmod.java:75 : '}' expected
          }
          ^
          1 error
          The answer is in the line number itself. It says that one of the matching brackets are missing in the program. Just check for the total number of starting and ending braces/brackets.

          Comment

          Working...