回文java_回文 Java
package test.HWtest;
import java.util.Scanner;
public class HWString {//判斷字符串
public static void hwReverse(String str){//方法一;將字符串倒置后逐一比較,
StringBuffer sb =new StringBuffer(str);
//System.out.println(sb);
sb.reverse();
//System.out.println(sb);
String newStr =new String(sb);
if(str.equals(newStr)){
System.out.println("1是回文");
}
else{
System.out.println("1不是回文");
}
}
public static void hwlength(String str){
StringBuffer sb =new StringBuffer(str);
//System.out.println(sb);
sb.reverse();
//System.out.println(sb);
int count=0;
for(int i=0;i
//數組中有length屬性,沒有length()方法
if(str.charAt(i)==sb.charAt(i)){
count++;
}
}
if(count == str.length()){
System.out.println("2是回文");
}
else{
System.out.println("2不是回文");
}
}
public static void main(String[] args) { //方法二 將字符串倒置后創建新字符串直接比較,
// TODO Auto-generated method stub
Scanner sc =new Scanner(System.in);
String str ="";
str =sc.nextLine();
hwReverse(str);
hwlength(str);
/*iamai
1是回文
2是回文*/}
}
總結
以上是生活随笔為你收集整理的回文java_回文 Java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注册界面php mysql_php:用户
- 下一篇: java enum判断_Java Enu