面试题,谢谢 写一个函数,检查字符串是否为整数,如果是,返回其整数值(Java), 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include<stdio.h> void main() { char t; int p; printf("请输入一个字符:\n"); scanf("%c",&t); if((t>'a'&&t<'z')||(t>'A'&&t<'Z')) printf("%c是整数",t); p=(int)t; printf("%c的整数值是%d",t,p); printf("%c不是整数",t); } import java.util.regex.*;public class TestInteger { static int check(String s){ Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher(s); while(m.matches()){ return Integer.parseInt(s); } return -1; } public static void main(String[] args) { String s="12315465"; int j=check(s); System.out.println("j="+j); String s1="adfasdf"; int k=check(s1); System.out.println("k="+k); }}输出结果:j=12315465k=-1 public int isInteger(String a){ try{ int a = Integer.parseInt(a) return a; }catch(Excepiton e){ //不是整数,怎么处理随便 }} 新手上路费了不少脑筋哦,嘿嘿,代码如下:public class TestInt { public static void main(String[] args) { String st="12345"; System.out.println(Test(st)); } static String Test(String st1){ for(int i=0;i<st1.length();i++){ if(st1.charAt(i)<='0'&&st1.charAt(i)>='9'){ break; } } return st1; }} 修改2#代码import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestInteger { static int check(String s) { Pattern p = Pattern.compile("[+-]?\\d+"); Matcher m = p.matcher(s); while (m.matches()) { return Integer.parseInt(s); } return -1; } public static void main(String[] args) { String s = "12315465"; int j = check(s); System.out.println("j=" + j); String s1 = "-22"; int k = check(s1); System.out.println("k=" + k); }} public class Test { public static void main(String[] args) throws Exception { String s = "-123"; Test.isInt(s); } public static void isInt(String s) { if(s.matches("[-,+]\\d+")) { System.out.println("字符串为整数"); }else { System.out.println("字符串不为整数"); } }} 正解、简单、有效,楼主多看看javaAPI哈,,大部分要实现的功能在里面都能找到方法来解决,, public boolean isInteger(String str){ try{ Integer.parseInt(a) return true; }catch(Excepiton e){ return false; } } Cast一下看能不能catch到异常就行了! import java.io.*;public class IsInteger{ public static void main(String[] args) throws Exception { while(true) { System.out.print("请输入整数字符串:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s=br.readLine(); StringBuffer sb=new StringBuffer(); for(int i=0;i<s.length();i++) { if((int)s.charAt(i)>57 ||(int)s.charAt(i)<48) { System.out.println("该字符串不是整数,请重新输入"); break; } else { sb.append(s.charAt(i)); } } if(s.length()==sb.length()) { System.out.println(s); } } }} 怎么都考虑的是“”字符串中本来就是int的情况万一是“32.25”这种情况呢? public static void isInt(String s) { if(s.matches("[-,+]\\d+")) { // if(s.matches("[-,+]?\\d+")) System.out.println("字符串为整数"); }else { System.out.println("字符串不为整数"); } } 将数据类型 varchar 转换为 numeric 时出错 java线程的运行(很简单的,帮下忙) java 如何 实现 动态 读取配置文件,请多指教,谢谢!! 调用! XML在Linux下解析报错,但在Window下支行正常,有没有懂的帮助看看 关于wait和notify的问题,帮忙解读一下程序! 有谁能告诉我这个程序哪个地方错了吗 安装好了jb8,但是crack里面的文件大不开,不能注册,怎么办? 如何让ie支持swing 请问JBuilder4.0以上版本软件在哪能下载到? CentOS Linux必须用光盘安装吗? 大家帮帮忙!
void main()
{
char t;
int p;
printf("请输入一个字符:\n");
scanf("%c",&t);
if((t>'a'&&t<'z')||(t>'A'&&t<'Z'))
printf("%c是整数",t);
p=(int)t;
printf("%c的整数值是%d",t,p);
printf("%c不是整数",t);
}
import java.util.regex.*;
public class TestInteger {
static int check(String s){
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher(s);
while(m.matches()){
return Integer.parseInt(s);
}
return -1;
} public static void main(String[] args) {
String s="12315465";
int j=check(s);
System.out.println("j="+j);
String s1="adfasdf";
int k=check(s1);
System.out.println("k="+k);
}
}输出结果:
j=12315465
k=-1
try{
int a = Integer.parseInt(a)
return a;
}catch(Excepiton e){
//不是整数,怎么处理随便
}
}
public static void main(String[] args) {
String st="12345";
System.out.println(Test(st)); } static String Test(String st1){
for(int i=0;i<st1.length();i++){
if(st1.charAt(i)<='0'&&st1.charAt(i)>='9'){
break;
}
}
return st1;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TestInteger {
static int check(String s) {
Pattern p = Pattern.compile("[+-]?\\d+");
Matcher m = p.matcher(s);
while (m.matches()) {
return Integer.parseInt(s);
}
return -1;
} public static void main(String[] args) {
String s = "12315465";
int j = check(s);
System.out.println("j=" + j);
String s1 = "-22";
int k = check(s1);
System.out.println("k=" + k);
}
}
public class Test {
public static void main(String[] args) throws Exception {
String s = "-123";
Test.isInt(s);
}
public static void isInt(String s) {
if(s.matches("[-,+]\\d+")) {
System.out.println("字符串为整数");
}else {
System.out.println("字符串不为整数");
}
}
}
try{
Integer.parseInt(a)
return true;
}catch(Excepiton e){
return false;
}
}
import java.io.*;public class IsInteger
{
public static void main(String[] args) throws Exception
{
while(true)
{
System.out.print("请输入整数字符串:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
if((int)s.charAt(i)>57 ||(int)s.charAt(i)<48)
{
System.out.println("该字符串不是整数,请重新输入");
break;
}
else
{
sb.append(s.charAt(i));
}
}
if(s.length()==sb.length())
{
System.out.println(s);
}
} }}
万一是“32.25”这种情况呢?
public static void isInt(String s) {
if(s.matches("[-,+]\\d+")) { // if(s.matches("[-,+]?\\d+"))
System.out.println("字符串为整数");
}else {
System.out.println("字符串不为整数");
}
}