我定义了一个随机数,我想检查它是否是回文java
I defined a random number and i want to check if it is a palindrome java
import java.util.Random;
public class Loop6 {
public static void main(String[] args) {
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0) {
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
}
System.out.println("rev is : " + "" + rev);
if(value==rev) {
System.out.println("Palindrome");
}
}
}
Java 具有反转字符串的内置方法。
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
解决方案:
public class Test {
public static void main(String[] args) {
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0) {
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
}
System.out.println("Reversed Number : " + " " + result);
if (result != value) {
System.out.println("Not Palindrome");
} else {
System.out.println("Palindrome");
}
}
}
谢谢:)
你的代码几乎没问题,只是你更新了初始随机数,这不是你想要的。为了尽量减少对代码的更改,我建议您添加一个新变量并将其与反向算法的结果进行比较。
import java.util.Random;
public class Loop6 {
public static void main(String[] args) {
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0) {
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
}
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) { // compare the initial variable and the reverse result
System.out.println("Palindrome");
}
}
}
import java.util.Random;
public class Loop6 {
public static void main(String[] args) {
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0) {
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
}
System.out.println("rev is : " + "" + rev);
if(value==rev) {
System.out.println("Palindrome");
}
}
}
Java 具有反转字符串的内置方法。
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
解决方案:
public class Test {
public static void main(String[] args) {
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0) {
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
}
System.out.println("Reversed Number : " + " " + result);
if (result != value) {
System.out.println("Not Palindrome");
} else {
System.out.println("Palindrome");
}
}
}
谢谢:)
你的代码几乎没问题,只是你更新了初始随机数,这不是你想要的。为了尽量减少对代码的更改,我建议您添加一个新变量并将其与反向算法的结果进行比较。
import java.util.Random;
public class Loop6 {
public static void main(String[] args) {
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0) {
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
}
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) { // compare the initial variable and the reverse result
System.out.println("Palindrome");
}
}
}