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