写一静态方法

csm810902 2010-03-25 04:19:56
写一静态方法:传入一参数,判断一个数是否全是由数字组成,如果是,则返回“是由数字组成”,如果否,则返回“不是由数字组成”!谢谢了。
...全文
226 26 打赏 收藏 转发到动态 举报
写回复
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
tdgwj 2010-03-29
  • 打赏
  • 举报
回复
正则简单 不怕麻烦 用字符串的toCharArray() 方法获取字符数组
声明一个int型计数器初始为0
迭代判断 每一个元素 是否在0~9的ASCII码之间 如果不是则计数器++ 跳出循环
最后判断计数器>0 即非数字
小数、负数无非多了判断小数点、负号出现位置和个数
zhuyouyong 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 renxianzuo 的回复:]
最简单的办法,使用正则表达式
[/Quote]顶
computermajorsmc 2010-03-29
  • 打赏
  • 举报
回复
public static boolean isShuzi(String str){
try{
int x=Integer.paress(str)
//上面就是把一个字符串转化成一个Integer的那个方法,名字我记不住了
return true;
}catch(Exception e){
return false;
}
}
smc2java 2010-03-29
  • 打赏
  • 举报
回复
public static boolean checkNumber(String str) {
// return str.matches("^[\\d]+$");
try{
Long.parseLong(str);
}catch(Exception e){
return false;
}
return true;
}

public static void main(String[] args) {
String str = "2345768947376822331981";
if(checkNumber(str)){
System.out.println("true");
}else{
System.out.println("false");
}
}

j_tang 2010-03-29
  • 打赏
  • 举报
回复
如果不想用正则的话, 建议charAt() 一位一位的判断
最好不要用int 或者long
j_tang 2010-03-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 whut_lcy 的回复:]

引用 9 楼 tonytone2008 的回复:
public static boolean isShuzi(String str){
try{
int x=Integer.paress(str)
//上面就是把一个字符串转化成一个Integer的那个方法,名字我记不住了
return true;
}catch(Exception e){
return false;
}
}
……
[/Quote]

如果字符串长度比较长的话, 可能会有问题啊
无聊司马 2010-03-29
  • 打赏
  • 举报
回复
用正则对就是用这个 不过我现在还没学到这里来
xy_2005 2010-03-28
  • 打赏
  • 举报
回复
	public static boolean checkNumber(String str) {		
// return str.matches("^[\\d]+$");
try{
Long.parseLong(str);
}catch(Exception e){
return false;
}
return true;
}

public static void main(String[] args) {
String str = "2345768947376822331981";
if(checkNumber(str)){
System.out.println("true");
}else{
System.out.println("false");
}
}

用正则表达式,用异常若数字很大也会出错的
mutoujuelian 2010-03-27
  • 打赏
  • 举报
回复
我顶异常处理
ruoshui901 2010-03-27
  • 打赏
  • 举报
回复
用异常处理,就可以啦
luorong0729 2010-03-27
  • 打赏
  • 举报
回复
3 楼 正解
xiashuqiong123 2010-03-27
  • 打赏
  • 举报
回复
进来观摩一下而已
whut_lcy 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tonytone2008 的回复:]
public static boolean isShuzi(String str){
try{
int x=Integer.paress(str)
//上面就是把一个字符串转化成一个Integer的那个方法,名字我记不住了
return true;
}catch(Exception e){
return false;
}
}
思路:如果该字符串能够直接被转化成int,则返回tru……
[/Quote]

正则不见得每个人都会。还是这位的办法更可行,学过java的都知道。
summily 2010-03-27
  • 打赏
  • 举报
回复
用正则表达式
whd3331933 2010-03-27
  • 打赏
  • 举报
回复

public class isNumeric{
puclic static void main(String[] args){
String s1 = "89723457fakjhfas(*&897";
boolean question = isAllDigit(s1);
if(question){
System.out.println("是由数字组成");
}else{
System.out.println("不是由数字组成");
}
}
public static boolean isAllDigit(String num){
String s1 = num;
boolean b = true;
for(int i=0 ; i<s1.length() ; i++){
char c = s1.charAt(i);
if(c>='0' && c<='9'){
b = true;
} else{
b = fales;
break;
}
}
return b;
}
}


刚刚介入JAVA的时候是这么写 不过现在正则表达式 写这个很简单。
空~自由 2010-03-27
  • 打赏
  • 举报
回复
用正则表达式
woshayawo 2010-03-27
  • 打赏
  • 举报
回复
这个太简单了吧!!
tonytone2008 2010-03-27
  • 打赏
  • 举报
回复
public static boolean isShuzi(String str){
try{
int x=Integer.paress(str)
//上面就是把一个字符串转化成一个Integer的那个方法,名字我记不住了
return true;
}catch(Exception e){
return false;
}
}
思路:如果该字符串能够直接被转化成int,则返回true,否则返回false
sky123123 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ruoshui901 的回复:]

用异常处理,就可以啦
[/Quote]

……
cbsoftlover 2010-03-26
  • 打赏
  • 举报
回复
不会正则也行啊,用charAt(i)方法一个字符一个字符地判断呗
加载更多回复(6)
相关推荐

62,567

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告