Java 判斷輸入字符串是否為回文數;


String.charAt();

import java.util.Scanner;

/**
* @author yishengyouzai
* 功能實現: 判斷字符串是否是回文字符
* 實現方法:使用charAt();

* 日期:2019年3月5日上午8:46:23
*/
public class PalindromeCharacter {

public static void main(String[] args) {
  System.out.println("please outinput string:");
  Scanner in = new Scanner(System.in);
  String str = in.next();
  int start= 0;
  boolean palinderome = true;
  int end=str.length() - 1;


  while(start <= (str.length())/2) {

 

    if((str.charAt(start) != str.charAt(end - start)) && start!=end) {   // 我們判斷兩個下標 對應的字符串不同時來滿足條件
    palinderome = false;//定義為判斷回文數
    System.out.printf("%s\t%s\n",str.charAt(start),str.charAt(end-start));
  }   

    start++;//下標增加

    if(start == end) break; //如果兩個下標相同時,跳出循環
  }


    if(palinderome)
      System.out.println("是回文數");
    else
      System.out.println("不是回文數");
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM