Java 找到完美的正方形

Java 找到完美的正方形,java,while-loop,Java,While Loop,我试图编写一个循环,调用一个方法来确定输入的数字是否是一个完美的正方形。它编译得很好,所以我必须有一个逻辑错误,我的生活,虽然我找不到它。无论我输入多少,它似乎总是返回false,这让我相信问题在于isPerfect()方法。然而,我对java的了解还不够,还不知道从这里可以走到哪里。以下是我目前掌握的代码: public class Square { public static void main(String[] args) { int input

我试图编写一个循环,调用一个方法来确定输入的数字是否是一个完美的正方形。它编译得很好,所以我必须有一个逻辑错误,我的生活,虽然我找不到它。无论我输入多少,它似乎总是返回false,这让我相信问题在于isPerfect()方法。然而,我对java的了解还不够,还不知道从这里可以走到哪里。以下是我目前掌握的代码:

 public class Square
 {
     public static void main(String[] args)
     {
         int input = 0; //The default value for input
         Scanner keyboard = new Scanner(System.in);
         while (input != -1)
         {
             System.out.println("Please enter a number, or enter -1 to quit");
             input = keyboard.nextInt();
             if (isPerfect(input) == true) //Call isPerfect() to determine if is a perfect square
             {
                 System.out.println(input + " is a perfect square.");
             }
             else if(input == -1) //If equals exit code, quit
             {
                 break;
             }
             else //If it is not a perfect square... it's not a perfect square
             {
                 System.out.println(input + " is not a perfect square.");
             }
         }
     }




     public static boolean isPerfect(int input)
     {      
         double num = Math.sqrt(input); //Take the square root of the number passed

         if (((num * num) == input) && (num%1 == 1)) //If the number passed = it's roots AND has no remainder, it must be a perfect sqaure
         {
             return true; //Return true to the call
         }
         else
         {
             return false; //Return false to the call
         }
     }
 }
两个潜在问题

  • 带双精度的算术是不准确的。你可能想要

    int num = Math.round(Math.sqrt(input));
    
  • 你的无余数测试不符合你的想法
    (num%1==1)
    只测试
    n
    是否为奇数。而且你并不真的需要它。。您只需要
    if(num*num==input){…}


  • 在程序修复后,下面是整个代码:

    import java.util.Scanner;
    
     public class Square
     {
         public static void main(String[] args)
         {
             int input = 0; //The default value for input
             Scanner keyboard = new Scanner(System.in);
             while (input != -1)
             {
                 System.out.println("Please enter a number, or enter -1 to quit");
                 input = keyboard.nextInt();
                 if (isPerfect(input) == true) //Call isPerfect() to determine if is a perfect square
                 {
                     System.out.println(input + " is a perfect square.");
                 }
                 else if(input == -1) //If equals exit code, quit
                 {
                     System.out.println("Breaking!");
                     break;
                 }
                 else //If it is not a perfect square... it's not a perfect square
                 {
                     System.out.println(input + " is not a perfect square.");
                 }
    
             }
         System.out.println("Main complete!");
         }
    
    
        /**
            The isPerfect() method returns whether or not a number is a perfect square.
            @param input The input from the keyboard scanner, passed as an argument
        */
    
         public static boolean isPerfect(int input)
         {      
             int num = ((int)Math.sqrt(input)); //Take the square root of the number passed, as an integer
    
             if (num*num == input) //If the number passed = it's roots AND has no remainder, it must be a perfect sqaure
             {
                 return true; //Return true to the call
             }
             else
             {
                 return false; //Return false to the call
             }
         }
     }
    
    import java.util.Scanner;
    类完美
    {
    公共静态void main(字符串参数[])
    {
    整数计数=0;
    System.out.println(“输入任何数字”);
    扫描仪输入=新扫描仪(系统输入);
    int n=in.nextInt();
    对于(int i=1;ii*i)
    {
    计数++;
    系统输出打印项次(i*i);
    }
    }
    System.out.println(“有“+计数+完美数”);
    }
    }
    
    我在尝试#1时遇到了一个转换错误,尽管第二个似乎有效。现在我明白了,这比我尝试的错综复杂的巫术更有意义
    code:'Square.java:47:错误:不兼容类型:可能从double到int的有损转换
    `我修复了它,改为int num=((int)Math.sqrt(input));你能解释一下为什么这个代码是正确的吗?供将来参考。。。这是在2015年10月26日2点53分提出的。在未来,我会建议也许找到更多最近的职位?这个问题也已经解决了。谢谢你抽出时间,但正如我刚才所说,这个问题在3年前就解决了。
    import java.util.Scanner;
    class perfect
    {
        public static void main(String args[])
        {
            int count=0;
            System.out.println ("enter any number");
            Scanner in =new Scanner(System.in);
            int n=in.nextInt();
            for(int i=1;i<n;i++)
            {
                if(n>i*i)
                {
                   count++;
                   System.out.println( i*i);
                }
            }
            System.out.println("there are "+ count + " perfect numbers");
         }
    }