JAVA编程题.doc

上传人:11****ws 文档编号:2991134 上传时间:2019-05-16 格式:DOC 页数:13 大小:59KB
下载 相关 举报
JAVA编程题.doc_第1页
第1页 / 共13页
JAVA编程题.doc_第2页
第2页 / 共13页
JAVA编程题.doc_第3页
第3页 / 共13页
JAVA编程题.doc_第4页
第4页 / 共13页
JAVA编程题.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、JAVA 编程题1现在输入 n 个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供 resetimport java.util.*;public class bycommapublic static String splitStringByComma(String source)if(source=null|source.trim().equals(“)return null;StringTokenizer commaToker = new StringTokenizer(source,“,“);String result = new String

2、commaToker.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;for(int i = 0;i=0;i-)System.out.println(iii);2金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011

3、)(一千零一拾一元整)输出。package test.format;import java.text.NumberFormat;import java.util.HashMap;public class SimpleMoneyFormat public static final String EMPTY = “;public static final String ZERO = “零“;public static final String ONE = “壹“;public static final String TWO = “贰“;public static final String THRE

4、E = “叁“;public static final String FOUR = “肆“;public static final String FIVE = “伍“;public static final String SIX = “陆“;public static final String SEVEN = “柒“;public static final String EIGHT = “捌“;public static final String NINE = “玖“;public static final String TEN = “拾“;public static final String

5、 HUNDRED = “佰“;public static final String THOUSAND = “仟“;public static final String TEN_THOUSAND = “万“;public static final String HUNDRED_MILLION = “亿“;public static final String YUAN = “元“;public static final String JIAO = “角“;public static final String FEN = “分“;public static final String DOT = “.

6、“;private static SimpleMoneyFormat formatter = null;private HashMap chineseNumberMap = new HashMap();private HashMap chineseMoneyPattern = new HashMap();private NumberFormat numberFormat = NumberFormat.getInstance();private SimpleMoneyFormat() numberFormat.setMaximumFractionDigits(4);numberFormat.se

7、tMinimumFractionDigits(2);numberFormat.setGroupingUsed(false);chineseNumberMap.put(“0“, ZERO);chineseNumberMap.put(“1“, ONE);chineseNumberMap.put(“2“, TWO);chineseNumberMap.put(“3“, THREE);chineseNumberMap.put(“4“, FOUR);chineseNumberMap.put(“5“, FIVE);chineseNumberMap.put(“6“, SIX);chineseNumberMap

8、.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“, THOUSAND);chineseMoneyPattern.put(“4“, TEN_THOUSAND);chineseMoneyPattern.put(“5“, TEN)

9、;chineseMoneyPattern.put(“6“, HUNDRED);chineseMoneyPattern.put(“7“, THOUSAND);chineseMoneyPattern.put(“8“, HUNDRED_MILLION);public static SimpleMoneyFormat getInstance() if (formatter = null)formatter = new SimpleMoneyFormat();return formatter;public String format(String moneyStr) checkPrecision(mon

10、eyStr);String result;result = convertToChineseNumber(moneyStr);result = addUnitsToChineseMoneyString(result);return result;public String format(double moneyDouble) return format(numberFormat.format(moneyDouble);public String format(int moneyInt) return format(numberFormat.format(moneyInt);public Str

11、ing 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;StringBuffer cMoneyStringBuffer = new StringBuffer();for (int i = 0; i 0; i-)

12、 cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor);moneyPatternCursor = moneyPatternCursor = 8 ? 1 : moneyPatternCursor + 1;String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(“.“);cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf(“.“), cMo

13、neyStringBuffer.length();while (cMoneyStringBuffer.indexOf(“零拾“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零拾“), cMoneyStringBuffer.indexOf(“零拾“) + 2, ZERO);while (cMoneyStringBuffer.indexOf(“零佰“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零佰“), cMoneyStringBuf

14、fer.indexOf(“零佰“) + 2, ZERO);while (cMoneyStringBuffer.indexOf(“零仟“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零仟“), cMoneyStringBuffer.indexOf(“零仟“) + 2, ZERO);while (cMoneyStringBuffer.indexOf(“零万“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零万“), cMoneyStrin

15、gBuffer.indexOf(“零万“) + 2, TEN_THOUSAND);while (cMoneyStringBuffer.indexOf(“零亿“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零亿“), cMoneyStringBuffer.indexOf(“零亿“) + 2, HUNDRED_MILLION);while (cMoneyStringBuffer.indexOf(“零零“) != -1) cMoneyStringBuffer.replace(cMoneyStringBuffer.ind

16、exOf(“零零“), cMoneyStringBuffer.indexOf(“零零“) + 2, ZERO);if (cMoneyStringBuffer.lastIndexOf(ZERO) = cMoneyStringBuffer.length() - 1)cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length();cMoneyStringBuffer.append(fractionPart);result = cMoneyStringBuffer.toString();ret

17、urn result;private String addUnitsToChineseMoneyString(String moneyStr) String result;StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);int indexOfDot = cMoneyStringBuffer.indexOf(DOT);cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);cMoneyStringBuffer.insert(cMoneyStringBuffe

18、r.length() - 1, JIAO);cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);if (cMoneyStringBuffer.indexOf(“零角零分“) != -1)/没有零头,加整cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf(“零角零分“), cMoneyStringBuffer.length(), “整“);elseif (cMoneyStringBuffer.indexOf(“零分“) != -1)/没有零分,加整cMoneyStringB

19、uffer.replace(cMoneyStringBuffer.indexOf(“零分“), cMoneyStringBuffer.length(), “整“);else if(cMoneyStringBuffer.indexOf(“零角“)!=-1)cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf(“零角“),cMoneyStringBuffer.indexOf(“零角“)+2);/ tmpBuffer.append(“整“);result = cMoneyStringBuffer.toString();return result;p

20、rivate void checkPrecision(String moneyStr) int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1;if (fractionDigits 2)throw new RuntimeException(“金额“ + moneyStr + “的小数位多于两位。“); /精度不能比分低public static void main(String args) System.out.println(getInstance().format(new Double(10010001.01);

21、3、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么? 答:父类: package test; public class FatherClass public FatherClass() System.out.println(“FatherClass Create“); 子类: package test; import test.FatherClass; public class ChildClass extends FatherClass public ChildClass() System.out.println(“ChildClass Create“); public sta

22、tic void main(String args) FatherClass fc = new FatherClass(); ChildClass cc = new ChildClass(); 输出结果: C:java test.ChildClass FatherClass Create FatherClass Create ChildClass Create 4、内部类的实现方式? 答:示例代码如下: package test; public class OuterClass private class InterClass public InterClass() System.out.pr

23、intln(“InterClass Create“); public OuterClass() InterClass ic = new InterClass(); System.out.println(“OuterClass Create“); public static void main(String args) OuterClass oc = new OuterClass(); 输出结果: C:java test/OuterClass InterClass Create OuterClass Create 再一个例题: public class OuterClass private do

24、uble d1 = 1.0; /insert code here You need to insert an inner class declaration at line 3. Which two inner class declarations are valid?(Choose two.) A. class InnerOne public static double methoda() return d1; B. public class InnerOne static double methoda() return d1; C. private class InnerOne doubl

25、e methoda() return d1; D. static class InnerOne protected double methoda() return d1; E. abstract class InnerOne public abstract double methoda(); 说明如下: 一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。 故 A、B 错 二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。故 D 错 三.非静态内部类的非静态成员可以访问外部类的非静态变量。 故 C 正确 四.答案为 C、E 5

26、、Java 的通信编程,编程题(或问答),用 JAVA SOCKET 编程,读服务器几个字符,再写入本地显示? 答:Server 端程序: package test; import .*; import java.io.*; public class Server private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server() try ss=new ServerSocket(10000); while(true) socket

27、= ss.accept(); String RemoteIP = socket.getInetAddress().getHostAddress(); String RemotePort = “:“+socket.getLocalPort(); System.out.println(“A client come in!IP:“+RemoteIP+RemotePort); in = new BufferedReader(new InputStreamReader(socket.getInputStream(); String line = in.readLine(); System.out.pri

28、ntln(“Cleint send is :“ + line); out = new PrintWriter(socket.getOutputStream(),true); out.println(“Your Message Received!“); out.close(); in.close(); socket.close(); catch (IOException e) out.println(“wrong“); public static void main(String args) new Server(); Client 端程序: package test; import java.

29、io.*; import .*; public class Client Socket socket; BufferedReader in; PrintWriter out; public Client() try System.out.println(“Try to Connect to 127.0.0.1:10000“); socket = new Socket(“127.0.0.1“,10000); System.out.println(“The Server Connected!“); System.out.println(“Please enter some Character:“)

30、; BufferedReader line = new BufferedReader(new InputStreamReader(System.in); out = new PrintWriter(socket.getOutputStream(),true); o/2练习 1:写一个 Application 程序,让用户输入姓名后程序输出:”Welcome you,用户姓名”程序名:ApplicationGInOut.javaimport java.awt.*;import java.awt.event.*;public class ApplicationGInOutpublic static

31、 void main(String args)new FrameInOut( );clss FrameInOut extends Frame implements ActionListenerLabel prompt;Text Field input,output;Button btn;FrameInOut( )super(“Application 程序”);prompt=new Label(“请输入您的姓名:”);input=new TextField(10);output=new TextField(24);btn=new Button(“离开”);setLayout(new FlowLa

32、yout();add(prompt);add(input);add(output);add(btn);input.addActionListener(this);btn.addActionListever(this);setSize(350,220);show( );public void actionPerformed(ActionEvdnt e)if(e.getSource( )=input)output.setText(input.getText(“Welcom you,”+input.getText( )+”!”);elsedispose( );System.exit(0);练习 2:

33、写一个 Application 程序,输入三个数,输出其中的最大值和最小值.程序名:CallonMaxMin.javaimport java.awt.*;import java.awt.event.*;public class :CallonMaxMinpublic static void main(String args )new FrameMaxMin( );clss FrameMaxMin extends Frame implements ActionListenerLabel result;TextField in1,in2,in3;Button btn;float a=0.0F,b=

34、0.0F,c=0.0F,t=0.0F;FrameMaxMin( );super(“Application 找最大最上数程序”);result=new Label(“请输入三个数”);in1=new TextField(10);in2=new TextField(10);in3=new TextField(10);btn=new button(“确定”);add(in1);add(in2);add(in3);add(btn);add(result);btn.addActionListener(this);public void actionPerformed(ActionEvent e)a=float.parseInt(in1.getText( );b=float.parseInt(in2.getText( );c=float.parseInt(in3.getText( );if(ab)t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(bc)t=b;b=c;c=t;result.setText(“三数最大数为:”+c+”最小数为:”+a);练习 3:写一个 Applet 程序,输入三个数,输出其中的最大值和最小值.程序名:SeekMaxMin.java

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。