Topic: 帮忙写以下代码 |
Print this page |
1.帮忙写以下代码 | Copy to clipboard |
Posted by: bylmy Posted on: 2004-09-15 21:02 You are going to write a simple class called "Calculator" which has four methods called "plus","minus","mult" and "div".each of these methods takes two parameters(of type int) and carries out the appropriate function, printing the result to the screen. write that class Add a method called "getInput" which prints a message to the screen asking the user to choose which function they want applied and then asks for two integers.this method should call the appropriate method and then keep printing the message to the screen until the user decides to quit. Write another class called "Starter" which contains the "main"method. this method should construct an object of type "calculator" and then call the getInput method for that object. |
2.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: why Posted on: 2004-09-15 22:11 Please do your own homework!!! |
3.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: siage Posted on: 2004-09-16 11:48 用switch语句? |
4.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: allenlee Posted on: 2004-09-16 17:13 我想到io包……第二段好像麻烦一点。 |
5.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: siage Posted on: 2004-09-16 18:43 第二段是让你选择运算符号吧,可以用switch语句来判断输入的字符 switch(str) { case '+': ...... case '-': ...... . . . . } |
6.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: bylmy Posted on: 2004-09-16 18:47 谢谢大家 |
7.Re:帮忙写以下代码 [Re: siage] | Copy to clipboard |
Posted by: why Posted on: 2004-09-16 19:32 siage wrote: sigh.... switch only works for int, byte, short or char beware that when comparing String objects str1 == str2 doesn't work, use instead str1.equals(str2) http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#equals(java.lang.Object) |
8.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: allenlee Posted on: 2004-09-16 20:51 case '+': ...... case '-': 我也有看到一本书里面是这么写的,'+'不是一char常量吗????? |
9.Re:帮忙写以下代码 [Re: allenlee] | Copy to clipboard |
Posted by: why Posted on: 2004-09-16 23:05 allenlee wrote: but it's "switch(str)" 1. I suppose str is a String -- if it's not, don't name it as such 2. it should be switch (c) -- beware of the full-size Chinese characters 3. since it's for a beginner, better emphasize the break; i.e. switch (ch) { case '+': // ... break; case '-': // ... break; default: // ... } |
10.Re:帮忙写以下代码 [Re: bylmy] | Copy to clipboard |
Posted by: allenlee Posted on: 2004-09-17 10:39 i though str to be char str yesterday..... if i announce str is a char type,then switch(str) would be all right,right??? |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |