Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to repeat a while loop?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to repeat a while loop?

    I need my program to repeat the while loop if the student number = 0. How would you do this?

    class Main
    {
      public static void main( String args[] )
      {
        System.out.print("#Student Number:" );
        int student = BIO.getInt();
     
        System.out.print("#Coursework Mark:" );
        int coursework = BIO.getInt();
     
        System.out.print("#Exam Mark:" );
        int exam = BIO.getInt();
     
        while ( student > 0 )
        {
          double result = (double) (exam + coursework) / 2;
            System.out.print( "SN=");
            System.out.print( student );
            System.out.print( " EX=  ");
            System.out.print( exam );
            System.out.print( " CW=  ");
            System.out.print( coursework );
            System.out.print( " Mark =  ");
            System.out.println( result );
     
            System.out.print("#Student Number:" );
            student = BIO.getInt();
            System.out.print("#Coursework Mark:" );
            coursework = BIO.getInt();
            System.out.print("#Exam Mark:" );
            exam = BIO.getInt();
        }
    if ( student == 0)
    {
     
    }
      }
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to repeat a while loop?

    Can't you just do while(student >= 0)?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to repeat a while loop?

    My university want me to have the data be terminated when a 0 is entered.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to repeat a while loop?

    Quote Originally Posted by BITmixit View Post
    My university want me to have the data be terminated when a 0 is entered.
    Recommended reading: Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to repeat a while loop?

    Sridaran, please read this before you post again: The Problem with Spoon-feeding

    Further spoon-feeding will result in me banning you.

    Edit- Banned.
    Last edited by KevinWorkman; October 13th, 2011 at 09:43 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2011, 12:56 AM
  2. Wanting to repeat my program...
    By Shaybay92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2011, 09:29 AM
  3. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  4. repeat creation of text fields
    By kurt-hardy in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 12:05 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM