有道JAVA题,你能做吗

卖萌 2010-05-26 04:03:10
题目的大概意思是这样的:有一个字符串,如果字符串中的+,-号前后都是数字,则进行+,-计算(只考虑+,-计算,不考虑*,/),否则输出“非法字符串”:
譬如“asss+456”,则在控制台输出“非法字符”;
譬如“1a3+23d”,在控制台输出“非法字符”;
譬如“173-23d”,在控制台输出“非法字符”;
譬如“12+24”,在在控制台输出36
譬如“36-20”,在在控制台输出16
有能力的帮忙指导一下,谢谢了。。。
...全文
236 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjjk123 2010-05-28
  • 打赏
  • 举报
回复

public class CuteString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

new CuteString().parse("23+45");
}
private void parse(String str)
{
Pattern p=Pattern.compile("([0-9]+)([+-])([0-9]+)");
Matcher m=p.matcher(str);
String a="0";
String b="0";
String f="+";
if(m.matches())
{
a=m.group(1);
f=m.group(2);
b=m.group(3);

}
else
{
System.out.println("非法字符");
return;
}
int ia=Integer.parseInt(a);
int ib=Integer.parseInt(b);
System.out.println(f.equals("+"));
//int iff=Integer.parseInt(f);
int result=(f.equals("+")?(ia+ib):(ia-ib));
System.out.println("结果="+result);
}

}
hjjk123 2010-05-28
  • 打赏
  • 举报
回复
先判断
解析--输出

ok
山城忙碌人 2010-05-28
  • 打赏
  • 举报
回复
必须是j2me吗
卖萌 2010-05-28
  • 打赏
  • 举报
回复
那有时间就加我一下行吗,说起来我还只是个J2ME新手,就想多结识一些像你这样热情的人,我的QQ是303729470,呵呵。。。
qq84136264 2010-05-28
  • 打赏
  • 举报
回复
上班时间,没上QQ~
卖萌 2010-05-28
  • 打赏
  • 举报
回复
谢谢了,现在明白了,怎么加你的QQ你没有反应呢。。。
qq84136264 2010-05-28
  • 打赏
  • 举报
回复
public int indexOf(String str)


返回第一次出现的指定子字符串在此字符串中的索引。从左到右第一次出现的索引

参数:
str - 任意字符串。
返回:
如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这样的子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。
qq84136264 2010-05-28
  • 打赏
  • 举报
回复
/**
*调用者要保证str里面字符串的合法性(含有+或-操作符)
*/
void fun(String str)
{
boolean isplus = str.indexOf("+") != -1;//判断输入是“+”还是“-”,这里偷了点懒!如果输入字符串里面没有“+”,直接默认是“-” o(∩_∩)o 哈哈
int index = !isplus ? str.indexOf("-") : str.indexOf("+");//取得操作符位置
String tmp1 = str.substring(0,index);//取得操作数1
String tmp2 = str.substring(index+1);//取得操作数2
int int1,int2;
try{
int1 = Integer.parseInt(tmp1);//将取得的String类型操作数转化为int型
int2 = Integer.parseInt(tmp2);
}catch (Exception e) {//如果取得的操作数任意一个不是数字的话会抛出异常
System.out.println("非法字符");//提示非法字符
return;
}
int result = isplus ? int1+int2 : int1-int2;//操作数都是整形,进行运算
System.out.println(result);//打印结果
}
//以上实现仅仅使用了,java.lang包下面的Integer和String两个类,使用的方法也是J2ME支持的,而且简单明了,一般有点经验都应该能看明白吧?



卖萌 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 qq84136264 的回复:]
LZ? 你没试试我写的函数吗?
[/Quote]

试过了,这个方法还可以,但是str.indexOf("+") != -1这句话不太理解,英文API看不太明白,还请详解,谢谢。。。
卖萌 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shine333 的回复:]
正则表达式

Java code
String regex = "^\\d+[+-]\\d+$";
[/Quote]
试过了,这个方法还可以,但是str.indexOf("+") != -1这句话不太理解,英文API看不太明白,还请详解,谢谢。。。
RuanJava 2010-05-28
  • 打赏
  • 举报
回复
楼上那么多解答的,lz应该可以解决了!
宁静-夏天 2010-05-28
  • 打赏
  • 举报
回复
楼主的意思应该是PLO文法中的 自动解析四则运算,并包括提醒输入错误。

这方面需要以下步骤

1.词法分析,进行分词,获取分词如下:111,+,222,-,aaa
2.判断词元的合法性
3.进行进栈出栈的四则运算。这个是属于编译原理方面的算法。


chainhou 2010-05-27
  • 打赏
  • 举报
回复
楼主可以把接收到的串拆成单个字符,再用java的Character.isDigital()来判断,也可以用apache的common,StringUtils里面有一个方法是isNumberic,可以直接是否是数字。
山城忙碌人 2010-05-27
  • 打赏
  • 举报
回复
楼主:你的业务逻辑没说清楚,就是在这个字符串中,+-,是出现一次,还是几次,如果是一次。下面的代码会帮到你。
public class Test{
Test(String str) {
if (isTest(str) > 0) {
String cha = "" + str.charAt(isTest(str));
String[] rep = {};
if (cha.equals("+")) { // 加号需要转义
str = str.replaceAll("\\+", ",");
rep = str.split(",");
} else {
rep = str.split(cha);
}
try {// 将分割的字符串转换为整型,准备计算,如果在转换的过程中出现非字符,抛出异常
int first = java.lang.Integer.parseInt(rep[0]);
int last = java.lang.Integer.parseInt(rep[1]);
int num = 0;
if (cha.equals("+")) {
num = first + last;
} else {
num = first - last;
}
System.out.println("字符串合法" + num);
} catch (Exception e) {
System.out.println("字符串:" + str + "非法");
}
} else {
System.out.println("字符串:" + str + "非法");
}
}
/*
* 判断字符串中是否存在+,-如果没有直接说明这个是字符串是非法的 return + or -出现的位置
*/
int isTest(String str) {
int count = 0;
try {
boolean falg = str.contains("+");
boolean falg1 = str.contains("-");
if (falg) {
count = str.indexOf("+");
}
if (falg1) {
count = str.indexOf("-");
}
} catch (Exception e) {
System.out.println(123);
}
return count;
}
public static void main(String[] args) {
String str = "12+45";
Test csdn = new Test(str);
}
}
qq84136264 2010-05-27
  • 打赏
  • 举报
回复
LZ? 你没试试我写的函数吗?
bayougeng 2010-05-27
  • 打赏
  • 举报
回复
		Pattern p = Pattern.compile("^(\\d+[\\+-])+\\d+$");
Matcher m = p.matcher("123+356-93+874-89");
if(m.find()){
// 计算我就不写了。直接用\d+匹配出数字,再按符号计算就行。不用考虑优先级。
}

dr_lou 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 z303729470 的回复:]
引用 4 楼 dr_lou 的回复:
Java code

package com.xuz.datastruct.csdn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

/**
* @param args
*/
public stat……
[/Quote]

没弄过J2ME 不过这里面的东西都是J2SE的,是否J2ME也可以用啊。
卖萌 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dr_lou 的回复:]
Java code

package com.xuz.datastruct.csdn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

/**
* @param args
*/
public static……
[/Quote]


如果指定要用J2ME语言,该如何实现呢
dr_lou 2010-05-26
  • 打赏
  • 举报
回复

package com.xuz.datastruct.csdn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

/**
* @param args
*/
public static void main(String[] args) {
String str = "123+456";
String regex = "^\\d+[+-]\\d+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.matches()){
String[] temp = str.split("[+-]");
if (str.indexOf("+") > 0) {
System.out.println(Double.parseDouble(temp[0])+Double.parseDouble(temp[1]));
} else {
System.out.println(Double.parseDouble(temp[0])-Double.parseDouble(temp[1]));
}
} else {
System.out.println("匹配失败");
}
}

}



比较简陋。
kaynezhang 2010-05-26
  • 打赏
  • 举报
回复
正则表达式用于判断,然后使用JEXP或者JavaScript(Rhino)计算。
加载更多回复(2)

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧