如何在Java中检查一个数字是否为阳光数?

WBOY
WBOY 转载
2023-08-19 10:57:18 1047浏览

如何在Java中检查一个数字是否为阳光数?

如果输入数字的下一个值的平方根是任何数字的完全平方数,则称该数字为阳光数。

更进一步解释,如果我们给任何一个数加上1,我们就得到下一个值。然后我们需要找出它的平方根。如果我们得到一个整数值,那么我们可以说它是某个数的完全平方。如果我们确认下一个数有一个完全平方数,那么输入的数就是一个阳光数,否则它不是阳光数。

在本文中,我们将看到如何使用Java编程语言来检查一个数字是否是阳光数。

展示一些实例给你看

Instance-1

的中文翻译为:

实例-1

输入的数字是80。

让我们使用阳光数的逻辑来检查一下。

80的下一个值 = 80 + 1 = 81

81的平方根=9

正如我们在这里注意到的,81是9的完全平方。

因此,80是一个阳光的数字。

Instance-2

的中文翻译为:

实例-2

输入的数字是48。

让我们使用阳光数的逻辑来检查一下。

48的下一个值 = 48 + 1 = 49

49的平方根=7

正如我们在这里注意到的,49是7的一个完全平方数。

因此,48是一个阳光数。

Instance-3

的中文翻译为:

实例-3

输入的数字是122。

让我们使用阳光数的逻辑来检查一下。

122的下一个值= 122 + 1 = 123

123的平方根=11.09053651

正如我们在这里注意到的,123没有一个完美的平方数。

因此,122是一个阳光数。

其他一些阳光数的例子包括3、8、15、24、35、48、63等。

语法

要获取一个数字的平方根,我们可以使用java.lang包中的Math类中内置的sqrt()方法。

以下是使用该方法获取任意数字的平方根的语法。

double squareRoot = Math.sqrt(input_vale)

你可以使用Math.floor()来找到最接近的整数值。

Math.floor(square_root)

算法

  • 步骤 1 - 通过初始化或用户输入获取一个整数。

  • 第二步 - 然后我们通过将其加1来找到它的下一个值,并将其存储在另一个变量中。

  • 第三步 - 我们找到下一个值的平方根。

  • 第四步 - 现在我们正在寻找最接近的完全平方根,并将其与下一个平方根值相减。

  • 步骤 5 - 如果减法后的值为零,则我们将得到确认,它是一个整数值,表示下一个值是任意数字的完全平方。

  • 第6步 − 如果我们得到确认下一个数字是一个完全平方数,则打印该数字是一个阳光数,否则它不是一个阳光数。

多种方法

我们以不同的方式提供了解决方案。

  • 通过使用静态输入值

  • 通过使用用户定义的方法

让我们逐一查看程序及其输出。

方法一:通过使用静态输入值

在这种方法中,程序将初始化一个整数值,然后通过使用算法,我们可以检查一个数字是否是一个阳光数。

Example

的中文翻译为:

示例

import java.util.*;
public class Main{
   public static void main(String args[]){
         //declare an int variable and initialize with a static value
         int inputNumber=8;

         //declare a variable which store next value of input number
         double next=inputNumber + 1;

         //Find the square root of the next number
         //store it as double value
         double square_root = Math.sqrt(next);

         //check whether the square root is a integer value or not
         //if yes return true otherwise false
         if(((square_root - Math.floor(square_root)) == 0))

         //if true then print it is a sunny number
            System.out.println(inputNumber + " is a sunny number.");
         else
            //if true then print it is a sunny number
            System.out.println(inputNumber + " is not a sunny number.");
   }
}

输出

8 is not a sunny number.

方法二:通过使用用户定义的方法

在这种方法中,我们将一个静态值分配为输入数字,并将该数字作为参数传递给一个用户定义的方法,然后在方法内部,通过使用算法,我们可以检查该数字是否是一个Sunny数字。

Example

的中文翻译为:

示例

import java.util.*;
public class Main{
   public static void main(String args[]){
   //declare an int variable and initialize with a static value
   int inp=15;

   //call the user defined method inside the conditional statement
   if(checkSunny(inp))

   //if true then print it is a sunny number
      System.out.println(inp + " is a sunny number.");

   else
      //if true then print it is a sunny number
      System.out.println(" is not a sunny number.");
   }

   //define the user defined method
   static boolean checkSunny(int inputNumber){

      //declare a variable which store next value of input number
      double next=inputNumber + 1;

      //Find the square root of the next number
      // store it as double value
      double square_root = Math.sqrt(next);

      //check whether the square root is a integer value or not
      //if yes return true otherwise false
      return ((square_root - Math.floor(square_root)) == 0);
   }
}

输出

15 is a sunny number.

在本文中,我们探讨了如何使用三种不同的方法在Java中检查一个数字是否是阳性数。

以上就是如何在Java中检查一个数字是否为阳光数?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除