验证回文串(java)
2009-12-13 17:58
211 查看
import java.util.*;
import javax.swing.*;
/******************************************************
This is a progrem judge whether a word is palindrome
*******************************************************/
public class text1
{
public static void main(String[] args)
{
String judge="yes";//use for while
while(judge.equals("yes"))
{
String aword=JOptionPane.showInputDialog("Input a word:");
ReturnWord r=new ReturnWord(aword.toLowerCase());//build ReturnWord than init it
if(r.testReturnWord())//if aword is a palindrome show message
JOptionPane.showMessageDialog(null,aword+" is a palindrome.");
if(!r.testReturnWord())
JOptionPane.showMessageDialog(null,"Error! "+aword+" is not a palindrome!");
//suggestive words
judge=JOptionPane.showInputDialog("Do you want again?(Input yes or no ):");
if(!judge.equals("yes")&&!judge.equals("no"))
JOptionPane.showMessageDialog(null,judge+" is Error! "+"system will quit!");
}
System.exit(0);//exit
}
}
// palindrome class
class ReturnWord
{
public ReturnWord(String word)
{
this.word=word;
}
//test word wether a palindrome
public boolean testReturnWord()
{
int i=0,j=word.length()-1,k=1;
while(i<=j)
{
if(word.charAt(i)>='a'&&word.charAt(i)<='z')
{
while(j>=0)
{
if(word.charAt(j)>='a'&&word.charAt(j)<='z')
{
if(word.charAt(i)==word.charAt(j))
{
j--;
break;
}
else
{
k=0;
break;
}
}
j--;
}
}
if(k==0)break;
i++;
}
if(k==0)return false;
else return true;
}
private String word;
}
import javax.swing.*;
/******************************************************
This is a progrem judge whether a word is palindrome
*******************************************************/
public class text1
{
public static void main(String[] args)
{
String judge="yes";//use for while
while(judge.equals("yes"))
{
String aword=JOptionPane.showInputDialog("Input a word:");
ReturnWord r=new ReturnWord(aword.toLowerCase());//build ReturnWord than init it
if(r.testReturnWord())//if aword is a palindrome show message
JOptionPane.showMessageDialog(null,aword+" is a palindrome.");
if(!r.testReturnWord())
JOptionPane.showMessageDialog(null,"Error! "+aword+" is not a palindrome!");
//suggestive words
judge=JOptionPane.showInputDialog("Do you want again?(Input yes or no ):");
if(!judge.equals("yes")&&!judge.equals("no"))
JOptionPane.showMessageDialog(null,judge+" is Error! "+"system will quit!");
}
System.exit(0);//exit
}
}
// palindrome class
class ReturnWord
{
public ReturnWord(String word)
{
this.word=word;
}
//test word wether a palindrome
public boolean testReturnWord()
{
int i=0,j=word.length()-1,k=1;
while(i<=j)
{
if(word.charAt(i)>='a'&&word.charAt(i)<='z')
{
while(j>=0)
{
if(word.charAt(j)>='a'&&word.charAt(j)<='z')
{
if(word.charAt(i)==word.charAt(j))
{
j--;
break;
}
else
{
k=0;
break;
}
}
j--;
}
}
if(k==0)break;
i++;
}
if(k==0)return false;
else return true;
}
private String word;
}
相关文章推荐
- java正则表达式常用验证
- Java调取创蓝253短信验证码的实现代码
- dotnet调java webservice C#调java webservice 头部验证 SoapHeader
- 正则表达式验证工具类RegexUtils.java
- C#访问Java的WebService添加SOAPHeader验证的问题
- java.sql.SQLException: ORA-28040: 没有匹配的验证协议(12c或者12c rac)
- C#调用java-ws(带身份验证功能)
- Java处理正则验证手机号-详解
- Java锁是如何保证数据可见性的 (本文未经过技术多方验证,仅供留存)
- Spring security 4 使用java注解进行登陆验证
- Java实现邮箱验证
- java 关于httpclient 请求https (如何绕过证书验证)
- Java Web Filter之登录验证
- Java通过第三方平台发送和验证手机验证码
- Java验证邮箱有效性和真实性
- JAVA日期验证 正则表达式,包括润二月(转)
- java对话框 及与验证控件的冲突
- javaweb 简单验证过滤器防止未登录直接进入主页
- MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能
- 善用LDAP来做登陆验证(Java 中级)