Results 1 to 11 of 11
  1. #1
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default Re-writing program with break/continue

    Hello all.

    My Java isnt that great, I was just wondering if anybody could give me a bit of help. Basically I got given a program that has break and continue statements in it, and I re-write it so it works without them

    Code Given
    import java.util.*;

    public class BreakContinue {
    public static void main(String args[]) {
    int Number;
    Scanner sc = new Scanner(System.in);

    while (true) // seemingly an infinite loop
    {
    System.out.print("Enter a positive integer ");
    System.out.println("or 0 to exit ");
    Number = sc.nextInt();
    if (Number == 0)
    break;
    if (Number < 0)
    continue;
    System.out.print("Squareroot of " + Number);
    System.out.println(" = " + Math.sqrt(Number));
    //continue lands here at end of current iteration
    }
    //break lands here
    System.out.println("a zero was entered");
    }
    }

    So far I have tried doing

    My Code

    import java.util.*;

    public class BreakContinue {
    public static void main(String args[]) {
    int Number=0;
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter a positive integer or 0 to exit");
    Number = sc.nextInt();
    while (Number < 0) // seemingly an infinite loop
    {
    if (Number <0 ){

    System.out.println("Squareroot of " + Number);
    System.out.println(" = " + Math.sqrt(Number));
    }
    else if (Number == 0)
    {
    System.out.println("a zero was entered");
    }
    }
    }
    }


    When you enter in a number it is meant to calculate the squareroot of it, but if you typed in 0 then the program terminates.

    Whenever I type in a number it just asks me to enter in another number, if anybody could help it would be appreciated!

    Thanks

  2. #2
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    May I add the title is meant to be "without" not "with".

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Hello and welcome! Please use [code][/code] tags when posting code for easy reading.

    Are you doing this for an assignment? Because break and continue are perfectly valid ways of working with while loops (in fact, its the way I use while loops 90% of the time since they usually involve user input or file reading).

    Another way you can do it is by nesting the reading of user input into the while loop itself, like this pseudocode:
    Java Code:
    int input
    while((input equals userInput) not equal to 0){
        do something with input
    }
    Does that make sense?

  4. #4
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Ah okay i do apologise.

    Well its a task thats been set, that we have to re-write it so that it isnt using neither the break/continue statements.

    I've just edited the while so that
    Java Code:
    while (Number > 0)
    {
    if (Number > 0)
    It will calculate the square root of the number entered but the loop doesn't stop, it just keeps repeating "the square root is:" rather then ask for another number or pressing 0 to terminate.

    I still havent really got the hang of using loops so it doesnt make much sense to me.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Did you look at the example of an assignment inside a conditional that I provided?

  6. #6
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    I don't understand how to apply it to my program.

  7. #7
    mastal is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Try with while(Number!=0) ;)

  8. #8
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    When I use that it does the same as when while(Number > 0) which just gives an infinite amount of square root lines.
    I'm guessing i'm not terminating the loop properly.

    Java Code:
    import java.util.*;
    
    public class BreakContinue {
    	public static void main(String args[]) {
    		int Number=0;
    		Scanner sc = new Scanner(System.in);
    		
    		System.out.println("Enter a positive integer or 0 to exit");
    		Number = sc.nextInt();
    		while (Number!=0)
    		{	
    			System.out.println("Squareroot of " + Number);
    			System.out.println(" = " + Math.sqrt(Number));
    		}
    		System.out.println("Enter a positive integer or 0 to exit");
    		
    		if (Number==0)
    		{
    			System.out.println("a zero was entered");
    		}
    		
    			}
    		}
    I don't know if its something quite obvious that i'm doing wrong?

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I think you have to put this code inside while loop.
    Java Code:
        System.out.println("Enter a positive integer or 0 to exit");
        Number = sc.nextInt();
    Java Code:
    public class BreakContinue
    {
        public static void main(String args[])
        {
            //int Number=0;
            [b]int Number=-1;[/b]//Declare it -1 so it will not exit
    
            Scanner sc = new Scanner(System.in);
            while (Number!=0)
            {
                System.out.println("Enter a positive integer or 0 to exit");
                Number = sc.nextInt();
                System.out.println("Squareroot of " + Number);
                System.out.println(" = " + Math.sqrt(Number));
            }
            System.out.println("Enter a positive integer or 0 to exit");
    
            if (Number==0)
            {
                    System.out.println("a zero was entered");
            }
        }
    }
    OR at least change Number value because if the user did not enter ZERO(0) it will go inside while loop
    and inside while loop Number is not changing which means it will never be zero and your while loop will never exit.

  10. #10
    rich123 is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks. That has fixed the problem! When 0 is entered the program would try calculating the square root of 0 and then terminate, not sure if there was a more simplier way of preventing it calculating the square root of 0 but i've just put an if (Number > 0) statement before the Print Lines so therefore it doesnt show it.


    Thanks for all the help :)

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    put if statement before
    Java Code:
        System.out.println("Squareroot of " + Number);
        System.out.println(" = " + Math.sqrt(Number));
    Java Code:
        //Put if statement here. If number is not equal to zero
        System.out.println("Squareroot of " + Number);
        System.out.println(" = " + Math.sqrt(Number));

Similar Threads

  1. Replies: 10
    Last Post: 01-07-2011, 10:28 AM
  2. using from lable with break and continue
    By arefeh in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 12:52 PM
  3. Help getting my program to continue requesting info.
    By antdilla22 in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 10:44 PM
  4. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 10:28 PM
  5. Demonstration of break and continue keywords
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •