分享一个自己写的判断回文数的程序
import java.util.Scanner;class AntiNum{
int EN,TNT,FT,BT;
boolean AR;
AntiNum(){
System.out.println("Please input the number");
Scanner t = new Scanner(System.in);
int a = t.nextInt();
this.EN=a;
this.compute();
this.result();
}
void result(){
if(AR == true){
System.out.println(EN+" is a Palindrome");
}else{
System.out.println(EN+" is not a Palindrome");
}
}
void compute(){
FT =1;
BT = this.getlength(EN);
Mark:
while(BT >= FT){
if(this.taste(FT) == this.taste(BT)){
AR = true;
FT++;
BT--;
}else{
AR = false;
break;
}
}
}
int taste(int a){
int b;
if(a == 1){
b = EN%miC(a);
}else{
b = (EN%miC(a) - EN%this.miC(a - 1)) / this.miC(a - 1);
}
return b;
}
int miC(int b){
int a = 10;
int c = a;
if(b <= 0){
a = 0;
}else{
for(int d = 2;d <= b;d++){
a = a * c;
}
}
return a;
}
int getlength(int a){
Integer a1 = a;
int b = a1.toString().length();
return b;
}
}
class NumTest{
public static void main(String[] args){
AntiNum test = new AntiNum();
}
}
乍一看,,还真不知道你写的是啥.....可能确实是我太菜了,,不过感觉不应该那么麻烦吧
import java.util.Scanner;
class num_int
{
int num;
num_int(int num)
{
this.num = num;
}
boolean isprime()
{
int num2 = num, ys, sum=0;
while (num2>0)
{
ys = num2 % 10;
sum = sum * 10 + ys;
num2 = num2 / 10;
}
return num == sum;
}
boolean isprime2()
{
String num2 = String.valueOf(num);
String num2_ = num2;
boolean result = true;
int len = num2.length();
for(int i = 0;i < len;i++)
if(num2.charAt(i) != num2_.charAt(len-i-1))
{
result = false;
break;
}
return result;
}
}
public class Main
{
public static void main(String[] args)
{
System.out.println("Please Input a number:");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
num_int t = new num_int(num);
System.out.println(t.isprime());
System.out.println(t.isprime2());
}
}
{:1_932:}都是大婶 {:1_918:}给我写个红包外挂吧 呃,其实回文判断从来都没有那么麻烦,先转字符串,再分割成数组,再逆置,再判断是否相等就完了 Thonnn 发表于 2017-10-4 15:23
呃,其实回文判断从来都没有那么麻烦,先转字符串,再分割成数组,再逆置,再判断是否相等就完了
我还没学数组 字符串kotlin 提供了反转的方法 上面的代码换成kotlin的话
fun main(args: Array<String>) {
var scan = Scanner(System.`in`)
var num = scan.nextInt().toString()
print(num.reversed() == num)
}
页:
[1]