zoukankan      html  css  js  c++  java
  • 回文

    设计思想
    要判断是否为回文,要看是否为一个字符,如果为一个,那么是回文;如果有两个,就看第一个和最后一个是否相等,以此类推,前面加一,后面减一,可以设立一个开关变量,只要有一个不相等,就确定其不是回文。
    源程序代码
    package palindrome;
    import java.util.Scanner;
    import java.math.*;
    public class doit {

    public static void main(String[]args)
    {
    	Scanner sr = new Scanner(System.in);//定义scanner,等待输入
    	System.out.println("请输入:");
    	String pal = sr.nextLine();
    	if(pal.length()<=1)
    	{
    		System.out.println("true");
    	}
    	else if(pal.length()==2)
    	{
    		if(pal.charAt(0)==pal.charAt(pal.length()-1))
    		{
    			System.out.println("true");
    		}
    		else
    		{
    			System.out.println("flase");
    		}
    	}
    	else
    	{   int flag=1;
    	    int i=0;
    		while(i<pal.length())
    		{
    			if(pal.charAt(i)!=pal.charAt(pal.length()-i-1))
    			{
    				flag=0;
    			}
    			i++;
    		}
    		if(flag==1)
    		{
    			System.out.println("true");
    		}
    		else if(flag==0)
    		{
    			System.out.println("flase");
    		}
    	}
    }
    

    }
    运行结果截图

    编程总结
    一个问题,可以使用多种方法去解决;测试程序,可以让你发现其中的问题,来优化与完善程序。
    耗时:40min

  • 相关阅读:
    ESP8266 在线构建 固件
    NodeMCU 物联网开发快速上手
    vofaplus-plugins dtantdir
    欧拉角
    DebugView 简单使用
    Intrusion Detection Evaluation Dataset (CIC-IDS2017)
    ureport
    how to ignore server cert error in javamail
    465和587区别
    JavaMail 发送邮件阻塞问题解决——设置 smtp 超时时间
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/11585587.html
Copyright © 2011-2022 走看看