1、JAVA代码题精心收藏 JAVA代码查错1.abstract class Name private String name;public abstract boolean isStupidName(String name) 大侠们,这有何错误?答案: 错。abstract method必须以分号结尾,且不带花括号。2.public class Something void doSomething () private String s = ;int l = s.length();有错吗?答案: 错。局部变量前不能放置任何访问修饰符 (private,public,和protected)。fin
2、al可以用来修饰局部变量(final如同abstract和strictfp,都是非访问修饰符,strictfp只能修饰class和method而非variable)。3.abstract class Something private abstract String doSomething ();这好像没什么错吧?答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstractmethod封锁起来呢? (同理,abstract method前不能加final)。4.p
3、ublic class Something public int addOne(final int x) return +x;这个比较明显。答案: 错。int x被修饰成final,意味着x不能在addOne method中被修改。5.public class Something public static void main(String args) Other o = new Other();new Something().addOne(o);public void addOne(final Other o) o.i+;class Other public int i;和上面的很相似,都是
4、关于final的问题,这有错吗?答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference(比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o的member vairable(成员变量),而o的reference并没有改变。6.class Something int i;public void doSomething() System.out.println(i = + i); 有什么错呢? 看不出来啊。答案: 正确。输出的是i = 0。int i属於instant variab
5、le (实例变量,或叫成员变量)。instant variable有default value。int的default value是0。7.class Something final int i;public void doSomething() System.out.println(i = + i);和上面一题只有一个地方不同,就是多了一个final。这难道就错了吗?答案: 错。final int i是个final的instant variable (实例变量,或叫成员变量)。final的instant variable没有default value,必须在constructor (构造器)
6、结束之前被赋予一个明确的值。可以修改为final int i = 0;。8.public class Something public static void main(String args) Something s = new Something();System.out.println(s.doSomething() returns + doSomething();public String doSomething() return Do something .;看上去很完美。答案: 错。看上去在main里call doSomething没有什么问题,毕竟两个methods都在同一个cl
7、ass里。但仔细看,main是static的。static method不能直接call non-static methods。可改成System.out.println(s.doSomething() returns + s.doSomething();。同理,static method不能访问non-static instant variable。9.此处,Something类的文件名叫OtherThing.javaclass Something private static void main(String something_to_do) System.out.println(Do s
8、omething .);这个好像很明显。答案: 正确。从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。10interface Aint x = 0;class Bint x =1;class C extends B implements A public void pX()System.out.println(x);public static void main(String args) new C().pX();答案:错误。在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹配(就象在同时impo
9、rt java.util和java.sql两个包时直接声明Date一样)。对于父类的变量,可以用super.x来明确,而接口的属性默认隐含为 public static final.所以可以通过A.x来明确。11.interface Playable void play();interface Bounceable void play();interface Rollable extends Playable, Bounceable Ball ball = new Ball(PingPang);class Ball implements Rollable private String name
10、;public String getName() return name;public Ball(String name) this.name = name; public void play() ball = new Ball(Football);System.out.println(ball.getName();这个错误不容易发现。答案: 错。interface Rollable extends Playable, Bounceable没有问题。interface可继承多个interfaces,所以这里没错。问题出在interface Rollable里的Ball ball = new B
11、all(PingPang);。任何在interface里声明的interface variable (接口变量,也可称成员变量),默认为public static final。也就是说Ball ball = new Ball(PingPang);实际上是public static final Ball ball = new Ball(PingPang);。在Ball类的Play()方法中,ball = new Ball(Football);改变了ball的reference,而这里的ball来自Rollable interface,Rollable interface里的ball是public
12、 static final的,final的object是不能被改变reference的。因此编译器将在ball = new Ball(Football);这里显示有错。 JAVA编程题1现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供resetimport java.util.*;public class bycommapublic static String splitStringByComma(String source)if(source=null|source.trim().equals()return null;StringT
13、okenizer commaToker = new StringTokenizer(source,);String result = new StringcommaToker.countTokens();int i=0;while(commaToker.hasMoreTokens()resulti = commaToker.nextToken();i+;return result;public static void main(String args)String s = splitStringByComma(5,8,7,4,3,9,1);int ii = new ints.length;fo
14、r(int i = 0;is.length;i+)iii =Integer.parseInt(si);Arrays.sort(ii);/ascfor(int i=0;i=0;i-)System.out.println(iii);2金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)(一千零一拾一元整)输出。package test.format;import java.text.NumberFormat;import java.util.HashMap;public class SimpleMoneyFormat public static final String EMPTY =
15、 ;public static final String ZERO = 零;public static final String ONE = 壹;public static final String TWO = 贰;public static final String THREE = 叁;public static final String FOUR = 肆;public static final String FIVE = 伍;public static final String SIX = 陆;public static final String SEVEN = 柒;public stat
16、ic final String EIGHT = 捌;public static final String NINE = 玖;public static final String TEN = 拾;public static final String HUNDRED = 佰;public static final String THOUSAND = 仟;public static final String TEN_THOUSAND = 万;public static final String HUNDRED_MILLION = 亿;public static final String YUAN =
17、 元;public static final String JIAO = 角;public static final String FEN = 分;public static final String DOT = .;private static SimpleMoneyFormat formatter = null;private HashMap chineseNumberMap = new HashMap();private HashMap chineseMoneyPattern = new HashMap();private NumberFormat numberFormat = Numb
18、erFormat.getInstance();private SimpleMoneyFormat() numberFormat.setMaximumFractionDigits(4);numberFormat.setMinimumFractionDigits(2);numberFormat.setGroupingUsed(false);chineseNumberMap.put(0, ZERO);chineseNumberMap.put(1, ONE);chineseNumberMap.put(2, TWO);chineseNumberMap.put(3, THREE);chineseNumbe
19、rMap.put(4, FOUR);chineseNumberMap.put(5, FIVE);chineseNumberMap.put(6, SIX);chineseNumberMap.put(7, SEVEN);chineseNumberMap.put(8, EIGHT);chineseNumberMap.put(9, NINE);chineseNumberMap.put(DOT, DOT);chineseMoneyPattern.put(1, TEN);chineseMoneyPattern.put(2, HUNDRED);chineseMoneyPattern.put(3, THOUS
20、AND);chineseMoneyPattern.put(4, TEN_THOUSAND);chineseMoneyPattern.put(5, TEN);chineseMoneyPattern.put(6, HUNDRED);chineseMoneyPattern.put(7, THOUSAND);chineseMoneyPattern.put(8, HUNDRED_MILLION);public static SimpleMoneyFormat getInstance() if (formatter = null)formatter = new SimpleMoneyFormat();re
21、turn formatter;public String format(String moneyStr) checkPrecision(moneyStr);String result;result = convertToChineseNumber(moneyStr);result = addUnitsToChineseMoneyString(result);return result;public String format(double moneyDouble) return format(numberFormat.format(moneyDouble);public String form
22、at(int moneyInt) return format(numberFormat.format(moneyInt);public String format(long moneyLong) return format(numberFormat.format(moneyLong);public String format(Number moneyNum) return format(numberFormat.format(moneyNum);private String convertToChineseNumber(String moneyStr) String result;String
23、Buffer cMoneyStringBuffer = new StringBuffer();for (int i = 0; i 0; i-) cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor);moneyPatternCursor = moneyPatternCursor = 8 ? 1 : moneyPatternCursor + 1;String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.inde
24、xOf(.);cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf(.), cMoneyStringBuffer.length();while (cMoneyStringBuffer.indexOf(零拾) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(零拾), cMoneyStringBuffer.indexOf(零拾) + 2, ZERO);while (cMoneyStringBuffer.indexOf(零佰) != -1) cMoneyStringBuffe
25、r.replace(cMoneyStringBuffer.indexOf(零佰), cMoneyStringBuffer.indexOf(零佰) + 2, ZERO);while (cMoneyStringBuffer.indexOf(零仟) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(零仟), cMoneyStringBuffer.indexOf(零仟) + 2, ZERO);while (cMoneyStringBuffer.indexOf(零万) != -1) cMoneyStringBuffer.replac
26、e(cMoneyStringBuffer.indexOf(零万), cMoneyStringBuffer.indexOf(零万) + 2, TEN_THOUSAND);while (cMoneyStringBuffer.indexOf(零亿) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(零亿), cMoneyStringBuffer.indexOf(零亿) + 2, HUNDRED_MILLION);while (cMoneyStringBuffer.indexOf(零零) != -1) cMoneyStringBu
27、ffer.replace(cMoneyStringBuffer.indexOf(零零), cMoneyStringBuffer.indexOf(零零) + 2, ZERO);if (cMoneyStringBuffer.lastIndexOf(ZERO) = cMoneyStringBuffer.length() - 1)cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length();cMoneyStringBuffer.append(fractionPart);result = cM
28、oneyStringBuffer.toString();return result;private String addUnitsToChineseMoneyString(String moneyStr) String result;StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);int indexOfDot = cMoneyStringBuffer.indexOf(DOT);cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);cMoneyString
29、Buffer.insert(cMoneyStringBuffer.length() - 1, JIAO);cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);if (cMoneyStringBuffer.indexOf(零角零分) != -1)/没有零头,加整cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(零角零分), cMoneyStringBuffer.length(), 整);elseif (cMoneyStringBuffer.indexOf(零分) != -1)/没有零分,加整cMoneyStringBuffe
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1