| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 984 人关注过本帖
标题:[求助]小女子紧急求助!
只看楼主 加入收藏
昏昏头
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-18
收藏
 问题点数:0 回复次数:10 
[求助]小女子紧急求助!

编程遇到问题啦!

题目在这里:
1.1 Learning objectives
Using String methods
Using the matches method and regular expression
Using exception
1.2 Task
Write a program Calculator.java to implement a simple calculator of basic operations (addition, subtraction, multiplication and division) on integers.

The user may enter a string comprising an operand, a binary operator, and a second operand, with any number of space characters between them. There may be leading spaces and trailing spaces in the input.

If an invalid input is encountered, a message "Error in expression" is displayed and the calculation is skipped. Otherwise, the respective operation is carried out, and the result displayed. The user then enters another string. To end the calculation, the user enters 'q' (without any leading or trailing spaces).

The operand is an integer. Examples of valid operands are 23, 0, -123, but not 23.5 (not an integer), 51.0 (not an integer), +123 (unary plus not accepted). You may assume that the operands and the result are within the range of value of the int type.

You need to handle division-by-zero error so that your program does not crash when it happens.

A partial code is given:

import java.util.*;

class Calculator {

Scanner scanner = new Scanner(System.in);

//-----------------------------------------------------
// Data members
//-----------------------------------------------------
String expr;
int length;
int index;

//-----------------------------------------------------
// Constructor
//-----------------------------------------------------
public Calculator () {
}

//-----------------------------------------------------
// main method
//-----------------------------------------------------
public static void main (String [] args) {

Calculator cal = new Calculator();

// to be filled in

}

// You may add other methods here

}


The data members given in the partial code are just suggestions. You are free to use your own approach.

Please refer to the sample outputs below.

1.3 Sample runs
Sample run using interactive input:

$ javac Calculator.java
$ java Calculator
123 + 345
123 + 345 = 468
-20+13
-20 + 13 = -7
004 -10
4 - 10 = -6
12*-0011
12 * -11 = -132
30 / 4
30 / 4 = 7
123/-7
123 / -7 = -17
q

Another sample run that shows some invalid inputs:

$ java Calculator
xyz
Error in expression
a + b
Error in expression
12345
Error in expression
-10 *
Error in expression
12.3 + 32
Error in expression
1 + 2 + 3
Error in expression
+30 / 3
Error in expression
30 // 7
Error in expression
333 / 0
Division by zero error
333 / 4
333 / 4 = 83
q

1.4 Important notes

Ensure that the outputs of your program comform to the format of the sample outputs given.


很急啊!小女子先在这里谢谢各位了!

搜索更多相关主题的帖子: 小女子 
2006-10-18 21:53
mojianpo
Rank: 1
等 级:等待验证会员
帖 子:213
专家分:0
注 册:2005-7-6
收藏
得分:0 
我帮你做好了,上qq,传

我是莫建坡.电话:15910657130
2006-10-18 22:13
昏昏头
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-18
收藏
得分:0 

谢谢!请问恩人的QQ号码是?

2006-10-18 22:16
mojianpo
Rank: 1
等 级:等待验证会员
帖 子:213
专家分:0
注 册:2005-7-6
收藏
得分:0 
406223802

我是莫建坡.电话:15910657130
2006-10-18 22:25
昏昏头
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-18
收藏
得分:0 

原来是老乡!感动!

2006-10-18 22:26
神经塔
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1987
专家分:4
注 册:2006-7-1
收藏
得分:0 

俺的英文不好,可以翻译成中文吗!!我也想做一下!


[shadow=255,red,2]待到秋来九月八,我花开时百花杀。冲天香阵透长安,满城尽是黄金甲。 [/shadow]
2006-10-18 23:58
wxhwjsw
Rank: 1
等 级:新手上路
帖 子:274
专家分:0
注 册:2006-6-15
收藏
得分:0 
。。。。。就是要做一个计算器的东西。。。。。不过这个计算器好像要求还蛮多的。。。

一个人的快乐,不是因为他拥有的多,而是因为他计较的少.
2006-10-19 14:10
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
得分:0 
以下是引用昏昏头在2006-10-18 21:53:26的发言:

编程遇到问题啦!

题目在这里:
1.1 Learning objectives //学习使用项目
Using String methods //使用String方法
Using the matches method and regular expression //使用常用的表达试
Using exception //使用异常处理机制
1.2 Task //任务,作业
Write a program Calculator.java to implement a simple calculator of basic operations (addition, subtraction, multiplication and division) on integers.
//用java写一个计算器程序能进行简单的整数的加,减,乘,除运算

The user may enter a string comprising an operand, a binary operator, and a second operand, with any number of space characters between them. There may be leading spaces and trailing spaces in the input.
//用户可以输入一串字符表达试其中包括 操作数,操作符 和若干空格。

If an invalid input is encountered, a message "Error in expression" is displayed and the calculation is skipped. Otherwise, the respective operation is carried out, and the result displayed. The user then enters another string. To end the calculation, the user enters 'q' (without any leading or trailing spaces).
//如果输入的不是整数就抛出或显示“Error in expression"的信息,并跳过次此计算,否则就继续运行并显示结果。用户输入字符’q‘可以退出程序。
The operand is an integer. Examples of valid operands are 23, 0, -123, but not 23.5 (not an integer), 51.0 (not an integer), +123 (unary plus not accepted). You may assume that the operands and the result are within the range of value of the int type.
//操作数是整型的。例如:可以是23,0,-123但不能是23.5(不是整型),51.0(不是整型),+123(加号不被接收)。你要确保输入的操作数要在整型数据类型所能表示的范围之内。
You need to handle division-by-zero error so that your program does not crash when it happens.
A partial code is given:
//你需要在程序中写出代码捕捉除数为零的异常

import java.util.*;

class Calculator {

Scanner scanner = new Scanner(System.in);

//-----------------------------------------------------
// Data members
//-----------------------------------------------------
String expr;
int length;
int index;

//-----------------------------------------------------
// Constructor
//-----------------------------------------------------
public Calculator () {
}

//-----------------------------------------------------
// main method
//-----------------------------------------------------
public static void main (String [] args) {

Calculator cal = new Calculator();

// to be filled in

}

// You may add other methods here

}


The data members given in the partial code are just suggestions. You are free to use your own approach.

Please refer to the sample outputs below.

1.3 Sample runs
Sample run using interactive input:

$ javac Calculator.java
$ java Calculator
123 + 345
123 + 345 = 468
-20+13
-20 + 13 = -7
004 -10
4 - 10 = -6
12*-0011
12 * -11 = -132
30 / 4
30 / 4 = 7
123/-7
123 / -7 = -17
q

Another sample run that shows some invalid inputs:

//如果输入的不符合要求要显示提示信息例如

$ java Calculator
xyz
Error in expression
a + b
Error in expression
12345
Error in expression
-10 *
Error in expression
12.3 + 32
Error in expression
1 + 2 + 3
Error in expression
+30 / 3
Error in expression
30 // 7
Error in expression
333 / 0
Division by zero error
333 / 4
333 / 4 = 83
q

1.4 Important notes //特别注意的是:

Ensure that the outputs of your program comform to the format of the sample outputs given.
//确保你程序的输出和给出的例子的输出格式保持一致。

很急啊!小女子先在这里谢谢各位了!


♂ 死后定当长眠,生前何须久睡。♀
2006-10-20 14:39
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
得分:0 
上面的是我看过之后给出的简单的注释,我也不是很懂这些英文就是胡乱根据自己的理解注释了一下错误之处在所难免,希望对大家对题义的理解有帮助!

♂ 死后定当长眠,生前何须久睡。♀
2006-10-20 14:42
tblovecj
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2006-9-20
收藏
得分:0 
如果9楼的朋友翻译没错,那做这个计算器的要求并不高啊,只要求整数相运行,有小数就提示,我的计算器其它功能实现了,就是在最难点上卡住了,就是先等于再加,或者加了等于了再加,或者连加,使加号起到等于号的作用,再或者等于后连加,再或者连加后等于再连加,哎,,,只要能实在加,就能同样实现减乘除了,累啊!!!

2006-10-22 03:06
快速回复:[求助]小女子紧急求助!
数据加载中...
 
   



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

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