找到完美的方块

时间:2015-10-26 02:53:48

标签: java while-loop

我正在尝试编写一个循环来调用一个方法来确定输入的数字是否是一个完美的正方形。它编译得很好,所以我必须有一个逻辑错误,对于我的生活,虽然我找不到它。无论我输入的数字似乎总是返回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
         }
     }
 }

3 个答案:

答案 0 :(得分:2)

两个潜在问题。

  1. 使用双精度算术是不准确的。你可能想要

    int num = Math.round(Math.sqrt(input));
    
  2. 你的没有余数的测试并没有做你想象的... (num%1 == 1)只是测试n是否是奇数。你真的不需要它......你所需要的只是if( num*num == input) { ... }

答案 1 :(得分:0)

所以修复了程序,这里是整个代码:

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
         }
     }
 }

答案 2 :(得分:0)

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");
     }
}