gpt4 book ai didi

java - 我定义了一个随机数,我想检查它是否是回文java

转载 作者:行者123 更新时间:2023-12-02 10:10:20 25 4
gpt4 key购买 nike

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");

关于java - 我定义了一个随机数,我想检查它是否是回文java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com