Java 程序检查太阳数

编写一个 Java 程序来检查数字是太阳数还是非太阳数。任何数字(n)都可以是太阳数,如果它的后继数或下一个数字(n + 1)是一个完全平方数。例如,23 是一个太阳数,因为它的下一个数字 24(23 + 1)是 4 的完全平方数。

package NumPrograms;

import java.util.Scanner;

public class SunnyNumber1 {
	private static Scanner sc;
	
	public static void main(String[] args) {

		sc = new Scanner(System.in);	
		
		System.out.print("Enter Number to Check for Sunny Number = ");
		int sNum = sc.nextInt();

		if (perfectSquare(sNum + 1)) 
		{
			System.out.println(sNum +  " is a Sunny Number");
		}
		else 
		{
			System.out.println(sNum +  " is Not a Sunny Number");
		}
	}
	
	static boolean perfectSquare(double sNum) {
		double squareRoot = Math.sqrt(sNum);
		
		return ((squareRoot - Math.floor(squareRoot)) == 0);
	}
}
Java Program to Check Sunny Number

程序 接受起始和结束范围,并打印范围内的太阳数或 1 到 N 的太阳数。

package NumPrograms;

import java.util.Scanner;

public class Example2 {
	private static Scanner sc;
	
	public static void main(String[] args) {

		sc = new Scanner(System.in);	
		
		System.out.print("Enter Start and End Range = ");
		int start = sc.nextInt();
		int end = sc.nextInt();
		
		System.out.println("The List of Numbers from " + start + " to " + end);
		for(int i = start; i <= end; i++)
		{
			if (perfectSquare(i)) 
			{
				System.out.print(i +  "  ");
			}
		}
	}
	
	static boolean perfectSquare(int sNum) {
		if(Math.sqrt(sNum + 1) % 1 == 0)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
}
Enter Start and End Range = 1 1000
The List of Numbers from 1 to 1000
3  8  15  24  35  48  63  80  99  120  143  168  195  224  255  288  323  360  399  440  483  528  575  624  675  728  783  840  899  960