Java 如何检查一个数是否是Bouncy数
Bouncy数可以定义为其数字不按递增或递减顺序排列的正数。
也就是说,这些数字是无序的。
1到100之间没有Bouncy数。
为了给你展示一些示例
示例1
输入数为101
用Bouncy数的逻辑来检查它-
1 > 0 and 0 < 1
因此,101是一个弹跳数。
示例2
输入的数字是905
让我们通过使用弹跳数的逻辑来检查它-
9 > 0 and 0 < 5
因此,905 是一个 Bouncy 数。
示例3
输入的数字是 245
让我们使用 Bouncy 数的逻辑来检查它-
2 < 4 but 4 < 5 means all digits are in decreasing order.
因此,245不是一个Bouncy数。
其他一些Bouncy数的示例包括102、564、897等。
语法
我们可以通过使用内置的 toString() 方法将整数值转换为字符串值。
以下是该语法。
String str = Integer.toString(inputNumber)
我们可以使用 charAt() 方法从字符串中选择任意索引处的字符。以下是其语法。
char ch = str.charAt(0);
步骤
- 步骤1 - 通过初始化或用户输入获取整数。
-
步骤2 - 将标志设置为true,并检查数字是否按升序或降序排列。
-
步骤3 - 如果数字按升序或降序排列,则将标志设置为false。
-
步骤4 - 打印结果。
多种方法
我们提供了不同的方法来解决问题。
- 通过使用静态输入值
-
通过使用用户定义的方法和charAt()方法
让我们逐个查看程序及其输出。
方法1:通过使用静态输入值
在此方法中,程序将初始化一个整数值,然后通过使用算法来检查一个数字是否为弹跳数。
示例
import java.util.*;
public class Main{
//Main method
public static void main(String args[]){
//initialized a number
int num = 6549;
//printing the given number
System.out.println("Given number: "+num);
// Checks if the number is less than 100
if(num<100){
System.out.println("The number is not a bouncy number");
}
// Boolean values to check if the digits are sorted
boolean increasing = true, decreasing = true;
// Storing the number in a temporary variable and storing the first digit
int temp = num, prev = num % 10;
// Loops until the number becomes zero
while(temp != 0){
// Checks if it follows increasing order or not
if(temp%10>prev){
increasing = false;
break;
}
// Gets the last digit of the number
prev = temp % 10;
// Removes the last digit
temp /= 10;
}
// Resets the value
temp = num;
prev = num % 10;
while(temp != 0){
// Checks if it follows decreasing order or not
if(temp%10<prev){
decreasing = false;
break;
}
// Gets the last digit of the number
prev = temp % 10;
// Removes the last digit
temp /= 10;
}
// Prints the result
if(!increasing&&!decreasing)
System.out.println("The number is a bouncy number");
else
System.out.println(", The number is not a bouncy number");
}
}
输出
Given number: 6549
The number is a bouncy number
方法2:使用用户定义的方法和charAt()方法
在这种方法中,用户将被要求输入一个整数值,并将该数作为参数传递给一个用户定义的方法,然后在方法内部,通过使用算法,我们可以检查一个数字是否为Bouncy数字。
示例
public class Main{
// Checks if the digits are in increasing order
static boolean isIncreasing(int num){
// Converts the number to a string
String s = Integer.toString(num);
char dig;
boolean result = true;
// Loops over all the digits
for(int i = 0; i < s.length()-1; i++){
// Stores the digit at i th location
dig = s.charAt(i);
// Compares the digit with its next location
if(dig > s.charAt(i+1)){
// Sets the result to false and breaks out of the loop
result = false;
break;
}
}
return result;
}
// Checks if the digits are in decreasing order
static boolean isDecreasing(int num){
// Converts the number to a string
String s = Integer.toString(num);
char dig;
boolean result = true;
// Loops over all the digits
for(int i = 0; i < s.length()-1; i++){
// Stores the digit at i th location
dig = s.charAt(i);
// Compares the digit with its next location
if(dig < s.charAt(i+1)){
// Sets the result to false and breaks out of the loop
result = false;
break;
}
}
return result;
}
//user defined method to check bouncy number
static boolean bouncyNum(int num){
// Checks if the number is less than 100
if(num<100){
System.out.println("The number is not a bouncy number");
}
// Checks for sorted digits using the functions
if(!isIncreasing(num)&&!isDecreasing(num))
return true;
else
return false;
}
//main method
public static void main(String args[]){
//initialized a number
int num = 150;
//printing the given number
System.out.println("Given number: "+num);
// Calls the user defined method and stores the result in res variable
boolean res = bouncyNum(num);
// Prints the result
if(res)
System.out.println("The number is a bouncy number");
else
System.out.println("The number is not a bouncy number");
}
}
输出
Given number: 150
The number is a bouncy number
在这篇文章中,我们探讨了如何使用不同的方法来检查一个数字是否是Java中的弹跳数字。