跳转到主内容
极星编程网:以代码为星,赴技术山海!

如何在Java中检查一个数字是否是迷人的数字?

迷人数字可以定义为一个数字,与2和3相乘,然后与该数字本身连接后的结果包含从1到9的所有数字。 要使一个数字成为一个令人着迷的数字,它应该是三位数或三位数以上。 向您展示一些实例 实例1 输入数字为327 让我们用迷人数字的逻辑来检查一下 - 立即学习 “ Java免费学习笔记(深入) ”;
327 * 2 = 654 327 * 3 = 981
连接“654”+“981”+“327”= 654981327 因此,327 是一个令人着迷的数字。 实例2 输入数字为192 让我们用迷人数字的逻辑来检查一下 - 立即学习 “ Java免费学习笔记(深入) ”;
192 * 2 = 384 192 * 3 = 576
连接“384”+“576”+“192”= 384576192 因此,327 是一个令人着迷的数字。 实例3 输入数字为241 让我们用迷人数字的逻辑来检查一下 - 立即学习 “ Java免费学习笔记(深入) ”;
241 * 2 = 482 241 * 3 = 723
连接“482”+“723”+“241”= 482723241 Eclipse导入Android或其他的JAVA项目的正确方法 WORD版 本文档主要讲述的是Eclipse导入Android或其他的JAVA项目的正确方法;希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看 下载 因此,241并不是一个令人着迷的数字 其他一些令人着迷的数字示例包括 192、1920、2019、327 等。 算法 第 1 步 - 通过初始化或用户输入获取整数。 第 2 步 - 检查这是否是三位数。 第 3 步 - 将数字乘以 2 和 3。 第 4 步 - 将两个乘积与数字本身连接起来。 第 5 步 - 现在查找数字中是否存在从 1 到 9 的所有数字。如果确实如此,那么这将是一个令人着迷的数字 多种方法 我们通过不同的方式提供了解决方案。 通过使用静态输入值 通过使用用户定义的方法 让我们一一看看该程序及其输出。 方法 1:使用静态输入值 在这种方法中,在程序中初始化一个整数值,然后通过使用算法我们可以检查一个数字是否是一个迷人的数字。 示例
public class Main { public static void main(String args[]){ // Initialized an integer value int num = 327; System.out.println("Given number: "+num); // Store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // Concatenate the numbers String concatNum = prod1+""+prod2+num; // Boolean value to store the result boolean flag = true; // Loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // COunt holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatNum.length(); i++) { char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } // Prints the result if(flag) System.out.println("Fascinating number"); else System.out.println("Not a fascinating number"); } }
输出
Given number: 327 Fascinating number
方法2:使用用户定义的方法 在这种方法中,在程序中初始化一个整数值,并将该数字作为参数传递到用户定义的方法中,然后在该方法中使用算法我们可以检查一个数字是否是一个迷人的数字。 示例
public class Mai { static boolean fascinatingNum(int num){ // Store the product of the numbers in the variables int prod1 = num*2; int prod2 = num*3; // Concatenate the numbers String concatNum = prod1+""+prod2+num; // Boolean value to store the result boolean flag = true; // Loops from 1 to 9 for(char c = '1'; c <= '9'; c++) { // COunt holds the number of times a digit occurs int count = 0; //loop counts the frequency of each digit for(int i = 0; i < concatNum.length(); i++){ char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) //increments the count by 1 if the specified condition returns true count++; } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } return flag; } public static void main(String args[]){ // Initialized an integer value int num = 327; System.out.println("Given number: "+num); // Calls the user defined method and stores the result in res variable boolean res = fascinatingNum(num); // Prints the result if(res) System.out.println("Fascinating number"); else System.out.println("Not a fascinating number"); } }
输出
Given number: 327 Fascinating number
在本文中,我们探讨了如何使用不同的方法在 Java 中检查一个数字是否是一个有趣的数字。

相关文章