Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem wont say if it is perfect Sqr Rt

JaxomJaxom Member Posts: 32
[code]

import java.util.Scanner;
import java.lang.Math;

public class PerfectSquare {

public static void main(String[] args) {
double number;
double srnum;

Scanner input = new Scanner(System.in);

System.out.print("Please enter a number to see if it can be squared: ");
number = input.nextDouble();

srnum = Math.sqrt(number);
System.out.println("The square root is: " + srnum);

if (number * number == srnum){
System.out.println("This is a perfect square.");
}else if (number * number != srnum){
System.out.println("This is not a perfect square.");
}
}
}
[/code]


Do you see anything wrong with this?

Comments

  • PerforatedPerforated Member Posts: 19
    : [code]
    :
    : import java.util.Scanner;
    : import java.lang.Math;
    :
    : public class PerfectSquare {
    :
    : public static void main(String[] args) {
    : double number;
    : double srnum;
    :
    : Scanner input = new Scanner(System.in);
    :
    : System.out.print("Please enter a number to see if it can be squared: ");
    : number = input.nextDouble();
    :
    : srnum = Math.sqrt(number);
    : System.out.println("The square root is: " + srnum);
    :
    : if (number * number == srnum){
    : System.out.println("This is a perfect square.");
    : }else if (number * number != srnum){
    : System.out.println("This is not a perfect square.");
    : }
    : }
    : }
    : [/code]
    :
    :
    : Do you see anything wrong with this?
    :


    First of all there are some code optimizations you could do and some minor errors. I've fixed it up and it looks better like this:

    [code]
    import java.util.Scanner;

    public class tst
    {
    public static void main(String[] args)
    {
    double number, srnum;

    Scanner input = new Scanner(System.in);

    System.out.print("Please enter a number to see if it can be squared: ");
    number = input.nextInt();

    srnum = Math.sqrt(number);
    System.out.println("The square root is: " + srnum);

    if (srnum * srnum == number)
    System.out.println("This is a perfect square.");
    else
    System.out.println("This is not a perfect square.");
    }
    }
    [/code]

    You don't need to import Math, it is always available, like strings for example. You also don't need two declarations of double. If you don't want/need to assign a value at the moment of declaration multiple variables of the same type can be declared like this.

    type variable1, variable2, ..., etc;

    You also have a rather fatal flaw in your code, you squared number (the one you took the square root of) and compared it to srnum which is the square root of number, what you should do is the following:

    if (srnum * srnum == number)

    and here comes another code optimization, if you only have one statement to execute after if there is no need for brackets and there is no need to write else if (whatever). If if is false there is no need for further chekcing is there? We already know that it's not a perfect square.

    Lastly the problem is that the computer doesn't store the exact values, so when you take the square root of number it won't be the exact square root unless it happens to be an integer square root like 2. I hope that this was of some help?
  • JaxomJaxom Member Posts: 32
    thanks that helps I normally do only one double but i was typing fast and must have done it outta instict.

    The If else i though might have made a difference cause I used to have an else thought I would try something new though...

    Thanks

    Summer
Sign In or Register to comment.