| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:[求助]编写四则运算时遇到的奇怪问题,想不明白
只看楼主 加入收藏
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
 问题点数:0 回复次数:3 
[求助]编写四则运算时遇到的奇怪问题,想不明白

题目的要求是:用户输入一个运算式后回车,出现结果.例如,输入"1+2",回车,输出3;输入"140/2",回车,输出70.以下是我的未完成的程序:

import java.io.*;

public class Calculation {
public static void main(String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int length=str.length();
int operand1,operand2,result=0;
String oper1="",oper2="";
boolean afterOper1=false;
char c,flag='0';
for(int i=0;i<length;i++){
c=str.charAt(i);
if(c!='+' && c!='-' && c!='*' && c!='/'){
if(!afterOper1){
oper1+=c;
}else{
oper2+=c;
}
}else{
afterOper1=true;
flag=c;
}
}
operand1=Integer.parseInt(oper1);
operand2=Integer.parseInt(oper2);
switch(flag){
case'+':result=operand1+operand2;break;
case'-':result=operand1-operand2;break;
case'*':result=operand1*operand2;break;
case'/':result=operand1/operand2;break;
}
System.out.println(result);
}
}
在这段代码里面,我只完成了两个操作数都是正数的情况.但是当操作数为负数时,我想加入符号位来判断.下面的代码是我修改后的代码:

import java.io.*;

public class Calculation {
public static void main(String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int length=str.length();
int operand1,operand2,result=0;
String oper1="",oper2="";
boolean afterOper1=false;
char c,flag='0';
for(int i=0;i<length;i++){
c=str.charAt(i);
if(c!='+' && c!='-' && c!='*' && c!='/'){
if(!afterOper1){
oper1+=c;
}else{
oper2+=c;
}
}else{
afterOper1=true;
flag=c;
}
}

c=oper1.charAt(0);//取出第一个操作数的首位来判断是数字还是负号
System.out.println(c);//当输入的第一个操作数有负号时,报错

operand1=Integer.parseInt(oper1);
operand2=Integer.parseInt(oper2);
switch(flag){
case'+':result=operand1+operand2;break;
case'-':result=operand1-operand2;break;
case'*':result=operand1*operand2;break;
case'/':result=operand1/operand2;break;
}
System.out.println(result);
}
}
上面的代码也是未完成,因为我想判断第一个操作数的符号时出现了错误.例如,当输入"-3+2"时,报错:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at src.Calculation.main(Calculation.java:28)
百思不得其解,请教高手为什么会这样?

搜索更多相关主题的帖子: 运算 编写 
2007-08-11 20:57
huwangvs
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:764
专家分:0
注 册:2007-2-4
收藏
得分:0 

你的if,else语句有问题。当第一个数是负数时,if语句不会执行的
oper1里什么都没有.

2007-08-12 11:14
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
原来如此,明白了,谢谢!
大家对这道题有什么好的方法吗?欢迎讨论!

I'm here, as always...
2007-08-12 19:56
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

完整程序已经做出来了:
import java.io.*;

public class Calculation {
public static void main(String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int length=str.length();
//operand1 is the 1st operand,operand2 is the 2nd
int operand1,operand2,result=0;
//oper1 is the 1st string of 1st operand,oper2 is the 2nd string of 2nd operand
String oper1="",oper2="";
//if afterOper1=true,operand2 will be counted,or operand1 will be counted
boolean afterOper1=false;
//flag is the operation
char c,flag='0';
//these two booleans are used to see whether the operands are minus
boolean firstMinus=false,secondMinus=false;
//to see whether the 1st operand is minus or not
c=str.charAt(0);
if(c=='-'){
firstMinus=true;
str=str.substring(1,length);
length--;
}
for(int i=0;i<length;i++){
c=str.charAt(i);
if(c!='+' && c!='-' && c!='*' && c!='/'){
if(!afterOper1){
oper1+=c;
}else{
oper2+=c;
}
}else{
afterOper1=true;
}
}
//get the operation
flag=str.charAt(oper1.length());
//to see whether the 2nd operand is minus or not
c=str.charAt(oper1.length()+1);
if(c=='-'){
secondMinus=true;
}
operand1=Integer.parseInt(oper1);
if(firstMinus) operand1=-operand1;
operand2=Integer.parseInt(oper2);
if(secondMinus) operand2=-operand2;
switch(flag){
case'+':result=operand1+operand2;break;
case'-':result=operand1-operand2;break;
case'*':result=operand1*operand2;break;
case'/':result=operand1/operand2;break;
}
System.out.println(result);
}
}
大家还有什么更好的方法吗?请教一下


I'm here, as always...
2007-08-12 22:49
快速回复:[求助]编写四则运算时遇到的奇怪问题,想不明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016437 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved